Issue with Response time from Parse - objective-c

Currently I am working with an iOS app which has some features to work with File datatype. I need to upload the images on Parse and also download the images from parse. Here I am facing issues with the response time that parse gives me.
When I upload a single image chosen from gallery in iOS device, it takes too much time to respond back. Likely if I am uploading a single image it takes around more than 1 minute to upload and respond back. The same thing is happening with downloading the images from parse server.
Can anyone please suggest me what should I do in order to improve these response time? Like do I need to pay something to get faster response? Or this is normal response time provided by parse for all accounts like paid and free.
I am using below code for uploading image,
//Upload a new picture
NSData *pictureData = UIImagePNGRepresentation(self.selectedImage);
PFFile *file = [PFFile fileWithName:#"img" data:pictureData];
[registrationData setObject:file forKey:#"image"];
[registrationData saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
} else {
}
}];
Can anyone please suggest me the way to get faster response?
Thanks in advance...!!!

Its due to Parse call multiple nested call to upload image(s) so its better to
use png image
scale image before uploading
type of account may of one of issue (free / paid)
hope It helps

Related

Send File in Chunks with Metadata

I am writing a web app and an OS X app that will upload files to a server.
The web app uses FlowJS to handle sending the files to the server. Along with every request, it sends the chunk number, chunk size, total file size, file name, etc. This is great because behind the scenes I'm uploading this data to an S3 and I use this information to determine when the file is finished uploading. Additionally, since the data is coming in chunks, I don't have to store the entire file in memory on my server.
For OS X with objective c, I'm planning on using AFNetworking. I tried using a multipart upload:
-(void)uploadFileWithUrl:(NSURL*)filePath {
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"http://www.example.com/files/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:#"blob" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
but this simply attempts to send the entire file in one request which is not acceptable as the server will run out of memory on large requests.
What I would like to know is if there is anything like FlowJS for objective C, or if there is some way to include that information with AFNetworking for each call.
I do not think that you can do this automatically very easily. (And if one knows how to do this, I would like to know the answer.)
Multipart is no help, because it structures the content, but not the transmission. Think of it as paragraphs in a text document: They give the text a structure, but it is still one file.
That leads to the real problem: You want to have chunks of a fixed size and send a chunk after one is processed (uploaded to S3), then the next chunk and so on. How does the client know that the first chunk is processed? HTTP does not work the way that you can send new data, when the first one received by the server. You send the next chunk, when the first one is sent (on the way). I'm not sure, whether AFNetworking guarantees to request for the next part, after sending the first part … Maybe it wants to collect small parts.
Why don't you do it, S3 does:
Initiate a upload request with an ID as response. Then send a chunk using that upload ID. If it is done, the server responds and the next part is send. Doing so you have a handshake.

AFNetworking won't allow large file upload

With AFNetworking I'm trying to upload an image (1280x990) with size: 33695. The code below works perfectly with smaller images (ie:390x390) but the larger image throws an error:
[client POST:#"/upload_image" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:#"image" fileName:#"image.jpg" mimeType:#"image/jpeg"];
} success:^(NSURLSessionDataTask * task, id responderData) {
} failure:^(NSURLSessionDataTask * task, NSError * error) {
}];
ERROR thrown:
NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";
I've searched many other posts and there doesn't seem to be anything referring to issues with a larger image size. Any suggestions?
Based on the author of AFNetworking, plz use appendPartWithFileURL instead. Because the data will be stream from disk.
I was having similar issues when I tried using AFNetworking. I have since switched to using RestKit and SDWebImage to handle the asynchronous loading and caching of images... and it works like a charm. You may want to take a look at this recent Quora post to better compare the differences between them.... Mainly the one con to Restkit was async and caching, but with little effort SDWebImage takes care of that with one line of code.
http://www.quora.com/iOS-Development/RestKit-vs-AFNetworking-What-are-the-pros-and-cons

Uploading From App to Server in IOS

I know that conventionally for an app to interact with the internet, it must use a web service to exchange information. However, how would one upload data(photos, text, audio recordings etc.etc.) from app to server(which holds data for all user accounts)? I know some people use an email-to-server tactic from research but even then it sounds ineffective and slow. How do apps such as Instagram upload so fast? I am trying to replicate that sort of uploading. Please guide me in the right direction.
Thanks for the help!
You should definitely look into AFNetworking. Here is an example of my uploading an image to a php web service:
NSData *imageData = UIImagePNGRepresentation(pageImage);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://www.SERVER.com"]];
//You can add POST parameteres here
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
author, #"author",
title, #"title",
nil];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:#"POST" path:#"/PATH/TO/WEBSERVICE.php" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
//This is the image
[formData appendPartWithFileData: imageData name:#"cover_image" fileName:#"temp.png" mimeType:#"image/png"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//Setup Upload block to return progress of file upload
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
NSLog(#"Upload Percentage: %f %%", progress*100);
}];
//Setup Completeion block to return successful or failure
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(#"response: [%#]",response);
//Code to run after webservice returns success response code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error: %#", [operation error]);
//Code to Run if Failed
}];
[operation start];
Edit- Also I use MBProgressHUD to display to the user the uploading on longer uploads.
As you might know, upload speed is always bound to the speed of the connection type you're using. Even the best upload technique will be slow when the connection is slow (GPRS for example, or EDGE, even 3G can be slow if network coverage is not good).
To upload large sets of data faster/better one thing you could do is compressing the data you're sending using ZIP or any other file compression format you wish or even develop you own compression algorithm (you might not want to do that ;-)).
If you want to reduce the overhead of HTTP/HTTPS connections for example, you can write your very own protocol for data exchange, implement it on the client/server side and upload faster. This will be a lot of work as you have to do all the implementation work not only for the protocol itself as you need to add security etc. But even if you choose to create a protocol, as said in the beginning, it will be slow if the connection is slow.
Update: A presenatation by Mike Krieger (Co-Founder of Instagram) where he covers your question just crossed my way https://speakerdeck.com/u/mikeyk/p/secrets-to-lightning-fast-mobile-design?slide=1.
The reason why you think it's so fast is, that they're updating the UI before the request (the Upload in this case) even completes. This is what Mike describes as "being optimistic". If the request fails you can still notify the user, but in the meantime make him feel productive and act like the request completed successfully.
This is a pretty open ended question but here are a few things to look at:
"Uploading fast" depends on the user's connection and server bandwidth so I won't get into that.
You can upload photos (and other files) by creating NSData objects and attaching them to a POST request. There is already a ton of sample code for uploading NSData but to convert a UIImage you will do the following:
NSData *imageData = UIImagePNGRepresentation(image);
You can do this using the built in Cocoa classes (NSMutableURLRequest) and with 3rd party networking classes (such as AFNetworking - just scroll down to file uploads).
When I send simple data to my webserver, I use the following approach: Use the ASIHttpRequest framework for connecting to your sever. Send the data in HTTP Post body, which is easy to do in the ASIHttpRequest framework. You will want to convert your data to either XML or JSON(use the SBJson framework for this) before sending it. I then write php scripts that parse the json or xml and then input this data into my database with custom SQL scripts. I can give you code snippets if you need them for any of these procedures...
It seems to me that, with your first sentence, you've basically answered your own question.
You need something on your server to receive the files and then you write client code to match. It could be as simple as ftp or as complex as a custom protocol depending on the security and control that you need.

Fastest way to retrieve a folder's content on Mac, in obj-c

Well, the title is quite explicit, but a little explantations for those interested in the background.
I'm developing a little image browser. On part of the application is a directory browser which allows me to browse all the folders of my hard drive and mounted volumes.
And while profiling, I noticed that the most time consuming method of my application was the following piece of code :
// get the content of the directory
NSFileManager * fileManager = [NSFileManager defaultManager];
NSURL * url = [NSURL fileURLWithPath:mPath];
mCachedContent = [[fileManager contentsOfDirectoryAtURL:url
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil] retain];
// parse the content, count the number of images and directories.
for (NSURL * item in mCachedContent)
{
if (CFURLHasDirectoryPath((CFURLRef)item))
{
++mNumChildren;
}
else if ([FileUtils isImage:[item path]] == YES)
{
++mNumImages;
}
}
This is necessary so that the NSOutlineView can know if a directory is expandable (and the number of images is also a feature I need)
To be more precise, the most time consuming method if [NSFileManager contentsOfDirectoryAtURL...]
So, is there any other way of getting a directory's content more efficient than the one I'm using ?
Thanks in advance for any help !
No matter how you write this function (e.g. with either Cocoa's NSFileManager API or the Unix opendir(3)/readdir(3) API), it's going to be I/O-bound—you're going to spend more time waiting on I/O than on any CPU operations performed in the middle layers.
If this is truly your bottleneck, then that means you're doing way too much I/O. Make sure you're not doing anything stupid like continually reading the contents of the same directory over and over again hundreds of times per second. If you need to continually watch a particular directory and take action whenever something in that directory changes (e.g. a file gets written to, a file is created or deleted, etc.), then use the File Systems Events API. This allows you to efficiently respond to those events when they happen without having to continually poll the directory.

Download an save an image file on HDD with Cocoa

I'm building a program, and I'm quite confident using Objective-C, but I don't know how to programmatically download a file from the web and copy it on the hard drive.
I started with :
NSString url = #"http://spiritofpolo.com/images/logo.png";
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
But then I don't know what to do with the data... that sucks, no ;)
Can somebody help?
You're close; the last thing you need is a call to -[NSData writeToFile:atomically:].
While that approach, with the final step provided by fbrereto, will work, it does not handle failure gracefully (indeed, it does not handle any sort of failure at all) and will block your application for the duration of the download.
Use NSURLDownload instead. It requires more code, but broken network connections, cut-off downloads, and inaccessible destination paths will not (necessarily) silently break your app.