问题
I'm having trouble understanding the difference betweeen [[NSManagedObjectContext defaultContext] save]
and [[NSManagedObjectContext defaultContext] saveInBackground]
.
When I use the save
functionality, it works just fine but saveInBackground
never persists the data. In short, this is the difference:
// Save in background.. DOES NOT WORK
[[NSManagedObjectContext defaultContext] saveInBackgroundErrorHandler:^(NSError *error){
NSLog(@"FAILED TO SAVE!! Task: %@", task);
}completion:^(void){
NSLog(@"PERSISTED NEW TASK: %@", task);
}];
// Save in main thread.. WORKS
[[NSManagedObjectContext defaultContext] save];
Everytime saveInBackground
runs, I see the "PERSISTED NEW TASK" log entry. However, when I restart the app, the data isn't there. When I use save
, I restart the app and the data is there. What am I missing?
回答1:
Had to call [context saveNestedContexts]
来源:https://stackoverflow.com/questions/12410213/magicalrecord-saveinbackground-not-persisting-data