iosobjective-casynchronousdispatch-asyncsendasynchronousrequest

Upload UIImage in dispatch_async


I have a problem. I call some method in dispatch_async. But in callMethod2 in different object I am uploading image with [NSURLConnection sendAsynchronousRequest. But after upload, It doesn't show me response. (However when I call callMethod2 without dispatch_async, it works great). Where can be the problem?

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
    [offline callMethod1];
    [offline callMethod2];
});

Upload image

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    NSLog("Never show me me this log");

}];

Solution

  • You're calling NSLog on a thread that isn't the main thread (aka calling it asynchronously) so you won't see NSLog as it must run on the main thread.

    You can have the completion block notify you some other way when it's done. The best way I've found is using https://github.com/kseebaldt/deferred which allows you to send a promise saying (I promise I'll do this thing and notify you when it's done).