I am building an app that requires JSON data. I am using a get call to a RESTful API and using and NSJSONSerialization to go through the data. In an effort to reduce start up time, I am logging the time everything takes to complete. After putting in the following log statements around this code, I noticed that this single call is taking on average exactly 5 seconds to complete. Any way that I can improve this time? I'm only getting 100 posts via JSON, so I can't imagine this is the fastest that it can move. Here is the code:
NSLog(@"Data Retrieval STARTED");
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:getURL]];
NSLog(@"Data Pull ENDED");
When I view the log, The first Log statement and the 2nd are always 5 seconds apart, so there's my test, not as accurate as Time Profiler, but I trust it. Tips are appreciated.
You should establish with curl (or similar) a baseline to compare the 5sec time to. Until then, I'd assume it's the server+transaction and nothing special related to iOS.
Anyway, no matter the delay, a remote request is too long to do synchronously while the app blocks. Instead, start by defining what amusing / useful thing your UI can do during the request (Maybe its a just spinner, or maybe its other startup tasks. Be thoughtful about this). Then create an NSURLRequest
from the NSURL
and pass that to an NSURLConnection
using:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
In the completion block, parse the response and restore your UI to "ready".