ios - Execute task after another -


recently started developing ios , faced problem maybe obvious couldn't figure out myself. i'm trying execute task after one, using multithreading provided gcd.

this code fetching json (put in class singleton) categoriesstore

- (instancetype)initprivate {     self = [super init];      if (self) {         [self sessionconf];          nsurlsessiondatatask *getcategories =         [self.session datataskwithurl:categoriesurl                     completionhandler:^(nsdata *data,                                         nsurlresponse *response,                                         nserror *error) {                         if (error) {                             nslog(@"error - %@",error.localizeddescription);                         }                          nshttpurlresponse *httpresp = (nshttpurlresponse *) response;                          if (httpresp.statuscode == 200) {                             nserror *jsonerror;                              nsarray *json =                             [nsjsonserialization jsonobjectwithdata:data                                                             options:nsjsonreadingmutablecontainers                                                               error:&jsonerror];                             if (!jsonerror) {                                 _allcategories = json;                                 nslog(@"allcategories - %@",_allcategories);                             }                         }         }];          [getcategories resume];     }      return self; } 

then in viewcontroller execute

- (void)fetchcategories {     nslog(@"before");      dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ {             categoriesstore *categories = [categoriesstore sharedstore];         dispatch_async(dispatch_get_main_queue(), ^(void) {             _alldirectories = categories.allcategories;             [self.tableview reloaddata];             nslog(@"after");         });     }); } 

-fetchcategories executed in viewdidappear. result before, after , json. want before, json after.

i tried dispatch_group_notify didn't workd.

how can working? why doesn't wait first task finished? thank's help! regards, adrian.

i suggest define dedicated method in categoriesstore fetches data remote server , takes callback argument:

- (void)fetchdatawithcallback:(void(^)(nsarray *allcategories, nserror* error))callback {      nsurlsessiondatatask *getcategories =         [self.session datataskwithurl:categoriesurl                     completionhandler:^(nsdata *data,                                         nsurlresponse *response,                                         nserror *error) {                         if (error) {                             nslog(@"error - %@",error.localizeddescription);                             callback(nil, error);                             return;                         }                          nserror *jsonerror = nil;                          nsarray *json =                         [nsjsonserialization jsonobjectwithdata:data                                                         options:nsjsonreadingmutablecontainers                                                           error:&jsonerror];                         if (!jsonerror) {                             _allcategories = json;                             nslog(@"allcategories - %@",_allcategories);                             callback(_allcategories, nil);                         } else {                             callback(nil, jsonerror);                         }         }];          [getcategories resume]; } 

and can use in viewcontroller:

- (void)fetchcategories {     [[categoriesstore sharedstore] fetchdatawithcallback:^(nsarray *allcategories, nserror* error) {         if (error) {             // handle error here         } else {             _alldirectories = allcategories;             [self.tableview reloaddata];         }     }] } 

in way reload table view after data loading & parsing.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -