I’ve had luck using dispatch queues to accomplish recurring events, 
specifically dispatch_after. For example:

- (void) doStuffAfterDelay
{
   dispatch_queue_t theQueue = dispatch_queue_create("Recurring work queue", 
DISPATCH_QUEUE_SERIAL);
    dispatch_time_t futureTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * 
NSEC_PER_SEC));
    dispatch_after(futureTime, theQueue,
    ^{
        DoStuff();

        [self doStuffAfterDelay];
    ;});
}

You can also use an NSTimer that repeats, such as:

NSTimeInterval delaySecs = 5;
self.timer = [NSTimer scheduledTimerWithTimeInterval:delaySecs
                        target:self selector:@selector(doStuff:)
                        userInfo:nil repeats:YES];

Hope this helps.

Doug Hill


> On May 13, 2016, at 12:34 PM, Carl Hoefs <newsli...@autonomy.caltech.edu> 
> wrote:
> 
> I want a method running on a background thread to reissue itself after it is 
> done processing. How can I do this?
> 
> I'm using the following code but it runs just once and is never heard from 
> again...
> 
>    // initial invocation from main thread
>    [self performSelectorInBackground:@selector(_checkSensor:) withObject:@50];
> 
> 
> // target method on background thread
> -(void)_checkSensor:(NSNumber *)delaySecs
> {
>    // check sensor...
>    // processing...
> 
>    // reschedule
>    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd 
> object:nil];
>    [self performSelector:_cmd withObject:nil afterDelay: delaySecs];
> }
> 
> As a test, the above works if run on the main thread, so I'm guessing it has 
> something to do with there being no runloop after the background thread 
> exits. 
> -Carl
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to