我正在开发一个应用,我希望每小时进行一次网络通话(同步目的)。即使应用程序处于终止状态。我怎样才能做到这一点?
任何地方都没有涉及UI更新,我只需要更新本地数据库和云存储文件。
答案 0 :(得分:1)
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;
});
}
答案 1 :(得分:0)
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");
}
我们已经创建了一个新的后台任务并为其添加了一个新线程,因此您可以自由地睡眠线程:) 我希望我能帮到你