我正在开发一个应用程序,在其中我想每小时进行一次网络呼叫(同步目的)。即使应用程序处于终止状态。我如何才能做到这一点?
没有涉及任何地方的UI更新,我只需要更新本地数据库和云存储文件。
发布于 2015-01-24 17:04:17
developer apple BackgroundExecution
-(void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}发布于 2015-01-24 21:03:01
if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
while(YES)
{
[NSThread sleepForTimeInterval:your_sleep_time];
}
});
}
else
{
NSLog(@"Multitasking Not Supported");
}我们已经创建了一个新的后台任务,并添加了一个新的线程,所以你可以自由地休眠线程:)我希望我已经帮助了你
https://stackoverflow.com/questions/28120531
复制相似问题