How to run NSTimer in background and sleep in iOS?

后端 未结 6 626
南笙
南笙 2021-02-04 21:56

I found a lot of post in stackoverflow about NSTimer run in background.

However I didn\'t find any solution.

In my app , I play sound in background

6条回答
  •  失恋的感觉
    2021-02-04 22:34

    When you run NSTimer, the @selector method itself will determine wether you want to run in background or main thread.

    Initial set up:

    self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(manageMethod) userInfo:nil repeats:YES]; //@property (nonatomic, strong) NSTimer *scanTimer
    

    Case you want to run in Background:

    -(void)manageMethod
    {
          dispatch_queue_t queue = dispatch_queue_create("com.mysite.thread1",NULL);
          dispatch_async(queue,^{ 
               //run in background
          });
    }
    

提交回复
热议问题