Call a method with delay of 15 sec irrespective of app state

此生再无相见时 提交于 2019-12-06 07:19:38

Calling it in App Delegate class is right place but it will not work for following cases.

  • It will not work if your app is killed from back ground.
  • It will not in background mode continuously. OS will stop that process after certain period of time.

-If the app is killed, you cannot do anything.

-When the app is in background, the OS may kill that process after certain time interval (I believe it is 15 seconds).

Though you can register for location changes, while the app is in background. In that case, your app will continue to receive location updates (such as for google maps).

-(void)callAfterFifteenSeconds {

    //1.) do your work


    //2.) If required, you can also choose to skip the next scheduling.
    BOOL shouldSchedule = YES;

    if (shouldSchedule) {
        //3.)
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //
            [self callAfterFifteenSeconds];

        });
    }

}
     [NSThread sleepForTimeInterval:4.0f];
     dispatch_async(dispatch_get_main_queue(),
                    ^{
               //write you code if you want fire any method after 4 sec//          

                    }
                    );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!