ios - Background and NSThread -


i'm trying understand how nsthread working when app going background. have following code in appdeligate:

- (void)applicationdidenterbackground:(uiapplication *)application   {       [self backgroundhandler];   }    - (void)backgroundhandler {        nsinteger counter=0;int scontinue=true;       while(scontinue){           nslog(@"counter:%d",counter++);           sleep(1)        }   }   

when go background prints out every 1second value. have kept open 5min , gave me:

counter:1   counter:2   ...   counter:300  

and keeps going. if try foreground backgroundhandler doesn't exit while loop , app doesn't respond anything.

now change applicationdideenterbackground , instead i'm using thread, i.e.

- (void)applicationdidenterbackground:(uiapplication *)application   {       [nsthread detachnewthreadselector:@selector(backgroundhandler) totarget:self withobject:nil];   }   - (void)backgroundhandler {       nsinteger counter=0;int scontinue=true;       while(scontinue){           nslog(@"counter:%d",counter++);           //sleep(1) : remove sleep shake of example       }   }   

although expecting have same behaviour in previous case thread seems hold after ms. had result is:

counter:1   counter:2   ...   counter:30  

and thread stucks @ point without executing anything. when go foreground thread starts running again, i.e. counter increases , being printed out. application runs again normally.

the above example rather simplistic version of i'm tyring do. want when app goes background communicate server long user doesn't go foreground. when goes communication should terminated. want combination of simple examples above, i.e. when go background in while loop keep asking server, , when enter foreground app start responding , terminate the backgroundhandler loop.

any help?

the main purpose of - (void)applicationdidenterbackground:(uiapplication *)application save state of application when application goes background. in if application starts using lot of cpu or ram, os terminate app based on state of phone.

when want perform background operations or services server enable background fetch calling [[uiapplication sharedapplication] setminimumbackgroundfetchinterval:uiapplicationbackgroundfetchintervalminimum]; in - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions of appdelegate.

-(void)application:(uiapplication *)application performfetchwithcompletionhandler:(void (^)(uibackgroundfetchresult))completionhandler {   //add code here  [self setbackgroundcheckinterval:uibackgroundfetchresultnewdata];     completionhandler(uibackgroundfetchresultnewdata); } - (void)setbackgroundcheckinterval:(nsinteger)resultcode {     uibackgroundrefreshstatus status = [uiapplication sharedapplication].backgroundrefreshstatus;     if (status == uibackgroundrefreshstatusavailable)     {         switch (resultcode)         {             case uibackgroundfetchresultfailed :                 [[uiapplication sharedapplication] setminimumbackgroundfetchinterval:background_check_interval_no_new_data];                 break;              case uibackgroundfetchresultnewdata :                 [[uiapplication sharedapplication] setminimumbackgroundfetchinterval:background_check_interval_new_data];                 break;              case uibackgroundfetchresultnodata :                 [[uiapplication sharedapplication] setminimumbackgroundfetchinterval:background_check_interval_no_new_data];                 break;         }     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -