objective c - How to return method object from inside block iOS -


return type of method nsarray, when call method nil or empty array. here it's below method implementation:

- (nsarray *)startparsing {     __block nsarray *array;     allproductsid = [[nsmutablearray alloc] init];     nsstring *string = [nsstring stringwithformat:@"http://%@:@%@",kprestashopapikey, kprestashopurlstring];     nsurl *url = [nsurl urlwithstring:string];      afhttprequestoperationmanager *manager = [[afhttprequestoperationmanager alloc] initwithbaseurl:url];     manager.responseserializer = [afxmlparserresponseserializer serializer];      [manager get:@"categories/21" parameters:nil success:^(afhttprequestoperation *operation, id responseobject) {         nsxmlparser *parser = (nsxmlparser *)responseobject;         [parser setshouldprocessnamespaces:yes];         parser.delegate = self;         [parser parse];         //nslog(@"first response %@", responseobject);         (int = 0; i< [[self.xmlshop objectforkey:@"product"] count]; i++) {             //nslog(@"second id --> %@", [self.xmlshop objectforkey:@"product"][i]);             nsstring *productid = [nsstring stringwithformat:@"products/%@", [[self.xmlshop objectforkey:@"product"][i] objectforkey:@"id"]];             [allproductsid addobject:productid];         }         array = [allproductsid copy];     } failure:^(afhttprequestoperation *operation, nserror *error) {         nslog(@"error occured %@", [error localizeddescription]);     }];     return array; } 

can me problem?

as quentin mentioned, can not directly because internally performing asynchronous request. means program starts request , continues next statements , not wait request finish. should either

  • make request synchronous cause program wait until request has finished. carefull not call method main thread since block app continuing respond until request returned. or
  • use block callback startparsing method (the same way blocks used actual request callbacks)

that loke example following:

- (void)startparsing:(void (^)(nsarray*))parsingfinished {     allproductsid = [[nsmutablearray alloc] init];     nsstring *string = [nsstring stringwithformat:@"http://%@:@%@",kprestashopapikey, kprestashopurlstring];     nsurl *url = [nsurl urlwithstring:string];      afhttprequestoperationmanager *manager = [[afhttprequestoperationmanager alloc] initwithbaseurl:url];     manager.responseserializer = [afxmlparserresponseserializer serializer];      [manager get:@"categories/21" parameters:nil success:^(afhttprequestoperation *operation, id responseobject) {         // parsing...         parsingfinished([allproductsid copy]);     } failure:^(afhttprequestoperation *operation, nserror *error) {         parsingfinished([[nsarray alloc] init]);          // or return nil or provide parsingfailed callback     }]; } 

which call like

[yourobject startparsing:^(nsarray *parseddata) {     // parsed data }]; 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -