ios - Stop a NSRunLoop in global queue -



ios - Stop a NSRunLoop in global queue -

i have created background task timer using nsrunloop , nstimer in viewcontroller:

- (void)runbackgroundtask: (int) time{ dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^{ nstimer* t = [nstimer scheduledtimerwithtimeinterval:time target:self selector:@selector(starttrackingbg) userinfo:nil repeats:yes]; [[nsrunloop currentrunloop] addtimer:t formode:nsdefaultrunloopmode]; [[nsrunloop currentrunloop] run]; }); }

to phone call function verify token validity, etc. possible end loop within function? instance:

-(void)starttrackingbg { if(token not valid) { stop_thread; dispatch_sync(dispatch_get_main_queue(), ^{ [self alertstatus:@"session lost!" :@"error!"]; [self poptologin]; }); } }

a couple of thoughts:

if @ documentation run, show pattern solves problem:

if no input sources or timers attached run loop, method exits immediately; otherwise, runs receiver in nsdefaultrunloopmode repeatedly invoking runmode:beforedate:. in other words, method begins infinite loop processes info run loop’s input sources , timers.

manually removing known input sources , timers run loop not guarantee run loop exit. os x can install , remove additional input sources needed process requests targeted @ receiver’s thread. sources hence prevent run loop exiting.

if want run loop terminate, shouldn't utilize method. instead, utilize 1 of other run methods , check other arbitrary conditions of own, in loop. simple illustration be:

bool shouldkeeprunning = yes; // global nsrunloop *therl = [nsrunloop currentrunloop]; while (shouldkeeprunning && [therl runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture]]);

where shouldkeeprunning set no somewhere else in program.

having said that, if going start thread, wouldn't utilize 1 of global worker threads, rather i'd instantiate own nsthread.

more critically, depending upon you're trying in other thread, there much improve other patterns establishing own run loop.

for example, if wanted have timer run in queue, i'd utilize dispatch timer instead:

@property (nonatomic, strong) dispatch_source_t timer;

and instantiate , start dispatch timer source run on designated gcd queue:

dispatch_queue_t queue = dispatch_queue_create("com.domain.app.polltimer", 0); self.timer = dispatch_source_create(dispatch_source_type_timer, 0, 0, queue); dispatch_source_set_timer(self.timer, dispatch_walltime(null, 0), kpollfrequencyseconds * nsec_per_sec, 1ull * nsec_per_sec); dispatch_source_set_event_handler(self.timer, ^{ <#code run upon timer event#> }); dispatch_resume(self.timer);

or, if want utilize nstimer, schedule on main runloop, , have method calls dispatch time consuming task background queue @ time. but, either way, i'd avoid adding overhead of sec run loop.

having shown improve ways utilize timers in background threads, describe intent (polling server) i'd recommend against using timer @ all. timers useful when want action initiated @ regular interval. in case, want initiate next server request after amount of time after previous request finished. so, in completion block of previous request, might like:

dispatch_after(dispatch_time(dispatch_time_now, 20.0 * nsec_per_sec), dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ <#code initiate next request#> });

also, i'd want create sure there very compelling reason polling server. polling seems intuitively appealing , logical, extravagant utilize of user's battery, cpu , info plan. , in cases need client respond server changes quickly, there improve architectures (sockets, force notifications, etc.).

ios objective-c grand-central-dispatch nstimer nsrunloop

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -