ios - invalidate NSTimer inside dispatch_async -
i creating game, , inside game need time counter.
i works great when scroll map stops , when finish scrolling start again. fix dispatch_async. here code
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ timer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(timenext) userinfo:nil repeats:no]; [[nsrunloop currentrunloop] addtimer:timer formode:nsdefaultrunloopmode]; [[nsrunloop currentrunloop] run]; });
timer:
__block nstimer *timer;
but if app goes background need stop times (and when active starts again).
with way can scroll map , timer works try:
- (void)timenext { [timer invalidate]; }
and didin't work. knows how stop it? or other idea accomplish task?
don't create timer on global queue's thread , start run loop on thread.
just create timer on main thread (main run loop), , add tracking run loop mode. timer can added multiple run loop modes.
timer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(timenext) userinfo:nil repeats:no]; [[nsrunloop currentrunloop] addtimer:timer formode:uitrackingrunloopmode];
Comments
Post a Comment