在我的应用中,我应该填充核心数据DB,我也想在后台进行
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
[application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
//Background tasks require you to use asyncrous tasks
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Perform your tasks that your application requires
DBSync *sync = [DBSync sharedInstance];
[sync startDownloadDataandUpdateDB];
[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
});
}
}
}
问题在于方法" startDownloadDataandUpdateDB"被称为,但这种方法在DBSync中调用其他方法,但这些方法不起作用,我不明白
答案 0 :(得分:0)
我认为在该方法中进行网络调用并不是一个好习惯。
The documentation 表示您有大约五秒的时间来执行任何任务并返回。
您可能需要查看beginBackgroundTaskWithExpirationHandler