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
Post a Comment