ios - Async request does not enter completion block -


the following code attempt me better understand [nsurlconnection sendasynchronousrequest:queue:completionhandler].

there nslog statements in completionhandler block, when run in main.m in xcode command line project, never enters completionhandler blocks. i've tried using different queues, mainqueue , currentqueue neither work.

my hunch queue being deallocated before request completed , retain cycles involved.

#import <foundation/foundation.h>  int main(int argc, const char * argv[]) {     @autoreleasepool {         nscache *mycache = [[nscache alloc] init];          nsarray *images = @[          @"http://i.stack.imgur.com/e66qr.png",          @"http://www.tiempoyquimera.com/wp-content/uploads/2010/01/euro-trash-girl-2010.jpg",          @"http://1.bp.blogspot.com/-mxd8ab2nbqy/uycisjiqz3i/aaaaaaaaah8/tc43u8aa9dm/s1600/tarantino10colhans_1460858i.jpg",          @"https://awestruckwanderer.files.wordpress.com/2014/02/alan-watts.png",          @"http://www.esalen.org/sites/default/files/photo_images/20120201_dellis__mg_9612_711.jpg"];          (nsstring *image in images){             nsurl *myurl = [nsurl urlwithstring:image];             nsurlrequest *request = [[nsurlrequest alloc] initwithurl:myurl];              nslog(@"can handle request %@", @([nsurlconnection canhandlerequest:request]));             nsoperationqueue *queue = [[nsoperationqueue alloc]init];              [nsurlconnection sendasynchronousrequest:request                                                queue:[nsoperationqueue mainqueue]                                    completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) {                                        nslog(@"in completion handler");                                        if (!error)                                        {                                            // save data cache url key                                            nslog(@"image added cache");                                            [mycache setobject:data                                                        forkey:myurl];                                        } else                                        {                                            nslog(@"image not added cache");                                        }                                    }];         }     }     return 0; } 

my hunch queue being deallocated before request completed , retain cycles involved

not quite. retain cycles not involved. persistence involved. doing in main function. exits immediately - asynchronous stuff (the networking , subsequent callback) asynchronous, come later, if had persistence. don't. main exits, , means whole darned program torn down, kaboom, before there opportunity networking, let alone call completion handler after networking.

now contrast how things happen in real life. in real ios app, main not exit, because calls uiapplicationmain, loops until app terminated.

int main(int argc, char *argv[]) {     @autoreleasepool {         return uiapplicationmain(argc, argv, nil,             nsstringfromclass([appdelegate class]));     } } 

in code, uiapplicationmain keeps running until aborted or otherwise terminated. meanwhile, classes , instance have sprung life, , persist, because uiapplicationmain not stop. example:

@implementation myviewcontroller - (void) somemethod {     // ...     [nsurlconnection sendasynchronousrequest:request          queue:[nsoperationqueue mainqueue]          completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) {              // ...     } } @end 

now, in 1 sense, same thing happens: somemethod exits immediately. our program overall still running! uiapplicationmain has run loop , run loop still cycling. thus, things live on, , asynchronous material can happen - can network , call callback.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -