【发布时间】:2014-11-11 06:14:20
【问题描述】:
我想在应用程序进入后台时保存数据。我正在取消 NSOperation 并将数据保存在 applicationDidEnterBackground 中。但它并没有完成执行。
如何在我的应用进入后台之前完成此操作?
代码:
-(void)applicationDidEnterBackground:(UIApplication *)application
{
//FUNCTION_START
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[self performSelectorOnMainThread:@selector(dispatchStateNotification:)
withObject:[NSNumber numberWithInt:666]
waitUntilDone:YES ];
// Write to core data
GWSCoreDataController *dataController = [GWSCoreDataController sharedManager];
NSError *error;
if (![dataController.managedObjectContext save:&error]) {
NSLog(@"Error while saving data to Core Data: %@", [error localizedDescription]);
}
// FUNCTION_END
}
-(void)dispatchStateNotification:(NSNumber *)value {
[[NSNotificationCenter defaultCenter] postNotificationName:APPLICATION_ENTERED_BACKGROUND_NOTIFICATION object:value];
}
【问题讨论】:
标签: ios iphone nsnotificationcenter nsoperation appdelegate