objective c - asynchronous function doesn't return data -


i wrote function fetch nsdictionary url.

[data loadfromapi:@"http://example.com/api" withsuccess:^(id data) {     nsmutabledictionary *result = (nsdictionary *)data;      cls_log(@"result: %@", result); } failure:^(nserror *error) {     cls_log(@"error: %@", error); }]; 

the function calls 1 below:

typedef void (^success)(id data); typedef void (^failure)(nserror *error);  + (void)loadfromapi:(nsstring *)apiurl withsuccess:(success)success failure:(failure)failure {     nsoperationqueue *operationqueue = [[nsoperationqueue alloc] init];      nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:apiurl]];      nsmutableurlrequest *mutablerequest = [request mutablecopy];      request = [mutablerequest copy];      afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request];     operation.responseserializer = [jsonresponseserializerwithdata serializer];      [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) {          nsarray *jsonarray = (nsarray *)responseobject ;          nsdictionary *result = [nsdictionary dictionarywithobjectsandkeys:                                [self now], @"time",                                jsonarray, @"response",                                nil];          cls_log(@"result is: %@", result);          success(result);     } failure:^(afhttprequestoperation *operation, nserror *error) {         failure?failure(error):nil;     }];      [operationqueue setmaxconcurrentoperationcount:1];     [operationqueue addoperations:@[operation] waituntilfinished:no]; } 

inside function cls_log(@"result is: %@", result); returns data returned. however, call function cls_log(@"result: %@", result); returns null. doing wrong?

as turns out [self now] returned null, nsdictionary wasn't being set properly.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -