앱이 백그라운드로 실행되면 데이터를 저장하고 싶습니다. NSOperation을 취소하고 데이터를 applicationDidEnterBackground
에 저장하려고합니다. 그러나 실행을 완료하지는 않습니다.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];
}
일부 코드를 공유하십시오. – Kampai