Question

I am creating a type ahead in my app that hits a remote rest service. The problem right now is that i am using a NSUrl to connect which is synchronous and because of the amount of looping through the json object it takes some time to parse. That is causing the UI to freeze while i search.

Therefore i decided to try to do:

[NSURLConnection sendAsynchronousRequest:myRequest
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *taxiData,    NSError *error) {

It seems to be a little better but still hangs because most of the processing is done in the completion handler which it tied to the main queue i assume.

So then i tried creating my own NSOperationsQueue or using dispatch_asynch:

[NSURLConnection sendAsynchronousRequest:myRequest
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *taxiData, NSError *error) {

           dispatch_queue_t typeAheadQueue = dispatch_queue_create("typeAhead", NULL);

           dispatch_async(typeAheadQueue, ^ { 

}];

and it works fine. The only problem is now it takes like 10 seconds for the uitableview to refresh the data. I can now type without any interruption but there is a huge delay in updating the uitableview.

Am i approaching this right? Any help is appreciated.

Thanks!

Was it helpful?

Solution

check this thread: run a process in the background while user can still use the UI

consider putting the entire nsurlconnection call in a separate queue, and when it comes time to update user interface, use get_main_queue like Mundi suggested.

OTHER TIPS

You can signal the main queue with

dispatch_async(dispatch_get_main_queue(), ^{
   [self.tableView reloadData];
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top