objective c - ios - get initial data from server on app start -


i'm new ios development , trying solve following problem.

in app (which speaks rest api) want make initial request server on app start user info. decided use separate service class singleton method. makes request server once , returns user instance.

@implementation lsshareduser  + (lsuser *)getuser {   // make request api server on first call   // on other calls return initialized user    static lsuser *_shareduser = nil;    static dispatch_once_t oncetoken;   dispatch_once(&oncetoken, ^{     lshttpclient *api = [lshttpclient create];     [api getuser:^(afhttprequestoperation *operation, id user) {         _shareduser = [[lsuser alloc] initwithdictionary:user];     } failure:nil];   });    return _shareduser; }  @end 

my question proper way of initializing global data server? see request async (with afnetworking lib) return null until request finished.

another problem here once failed (bad connection example) user null forever.

update code this

 static lsuser *_shareduser ; + (lsuser *)getuser {   // make request api server on first call   // on other calls return initialized user  if(_shareduser){  //this execute @ first time   static dispatch_once_t oncetoken;   dispatch_once(&oncetoken, ^{     lshttpclient *api = [lshttpclient create];     [api getuser:^(afhttprequestoperation *operation, id user) {         _shareduser = [[lsuser alloc] initwithdictionary:user];         return _shareduser;     } failure:nil];   }); } //this execute 2nd time   return _shareduser; } 

for answer line -> ques 2 )another problem here once failed (bad connection example) user null forever. ->once _shareduser initialized user _shareddata. until shared data not initialized return null whenever called.

ques 1 )my question proper way of initializing global data server? see request async (with afnetworking lib) return null until request finished.

a better way implement own custom delegate methods. once request fetched or when call work in delegate method. 1st time: execute calling delegate methods when request fetched or failed. 2nd time: after if block call delegate methods.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -