ios - Adding user before app actually start -


i'm trying add user first time app opened. adding response me sort of password, call access token. token using reaching api services.

anyway, beside story, problem first time app open, can add user , token , saved nsuserdefault, can't reach api services since tries reach services null. after refreshing or reopening app or switching view , view solve issue. first time, can't reach service.

the problem here, obviously, when app opens first time, before adding operation finished, try reach service cause stay null. here code have:

- (void)viewdidload {     [super viewdidload];      //adding user.     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; // user informations.     nsstring *accesstoken = [defaults objectforkey:@"accesstoken"];     if(accesstoken == nil)         [apiclient adduser];      //adding inital viewcontroller.     uiviewcontroller *vc = [self viewcontrollerforsegmentindex:self.typesegmentedcontrol.selectedsegmentindex];     [self addchildviewcontroller:vc];     vc.view.frame = self.contentview.bounds;     [self.contentview addsubview:vc.view];     self.currentviewcontroller = vc; } 

don't hang i'm doing while adding initial viewcontroller, rather please see happens there add user , initialize viewcontroller. being asynchronous problem, i'm looking way adding user before app started or launched.

i'm not sure in case if have add user in application:didfinishlauncingwithoptions, don't think problem.

okay, first thing extract token related code function in view controller. let's call tokenreceived:

-(void) tokenreceived{     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; // user informations.     nsstring *accesstoken = [defaults objectforkey:@"accesstoken"];     if(accesstoken == nil)         [apiclient adduser];      //adding inital viewcontroller.     uiviewcontroller *vc = [self viewcontrollerforsegmentindex:self.typesegmentedcontrol.selectedsegmentindex];     [self addchildviewcontroller:vc];     vc.view.frame = self.contentview.bounds;     [self.contentview addsubview:vc.view];     self.currentviewcontroller = vc; } 

in view did load add following:

[[nsnotificationcenter defaultcenter] addobserver:self         selector:@selector(ongettoken)          name:@"gettoken"         object:nil]; 

then add new method:

- (void) ongettoken {    [self tokenreceived] } 

and, ofcourse, when token ( in app delegate or wherever retrieve token ) send notification observer:

// code, saving nsuserdefualts etc. [[nsnotificationcenter defaultcenter]          postnotificationname:@"gettoken"          object:self]; 

explanation: bad practice block main thread network - app not respond of events , if don't have 3g or wifi - stuck. user experience bad, , drive users away.

on other hand, open user screen, , can put loader if want till notify view controller got something. way keep async nature of network , still refresh ui when one. if concerned token before opening view controller, can add tokenreceived view did load well.

one more thing, can send nsnotification object, , 1 nsnotification object, in case need notified happened, don't need data.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -