问题
This is the first time i've used the new version of RestKit and multiple MOC so this ones got my a little stumped. Im using RestKit 0.20-pre5 and am adding Managed Objects through RestKit operations as well as manually throughout the application.
RestKits provides two MOCs to manage performance: the mainQueue and PersistantStore Object contexts. From inspecting RestKits code all new managed objects are saved to the mainQueue MOC (Based on RKObjectManager line 433 and inspection of various pointer addresses). Objects created by RestKit get persisted fine (after calling a save of course) however whenever I create my own ManagedObjects in the same mainQueue MOC and save it, they do not persist when I relaunch the application. (I save the relevant MOC quite often)
This is where the persistant store comes in. Whenever I save the persistant store along with the mainQueue MOC the data persists. I imagine this has something to do with the RKManagedObjectStore merging changes whenever I call save on the persistantStoreMOC (via a notification) So... everything good right?
The problem is whenever I save both contexts there is a noticeable lag that lasts at least 1 second (it can vary up to 3!). This lag seems to increase the more NSManagedObjects I create. I have run instruments and there are no memory issues. I must be using RestKits implementation of core data incorrectly.
For the record I am saving using the following method:
- (void)saveContext
{
__block BOOL _blockSuccess;
__block NSError *_blockError;
NSManagedObjectContext *persistantContext = [[[RKObjectManager sharedManager] managedObjectStore] persistentStoreManagedObjectContext];
[globalManagedObjectContext performBlockAndWait:^{
_blockSuccess = [globalManagedObjectContext save:&_blockError];
}];
if (! _blockSuccess) NSLog(@"Failed to save context: %@", _blockError);
[persistantContext performBlock:^{
_blockSuccess = [persistantContext save:&_blockError];
if (! _blockSuccess) NSLog(@"Failed to save context: %@", _blockError);
}];
}
This is called from a singleton object whenever we want to save the state of the application. (globalManagedObjectContext is simply a macro pointing to RestKits mainQueueObjectContext). Removing the persistantContext save removes the lag but of course doesn't persist the data.
Anyones help (especially Blake Watters ;) would be greatly appreciated!
Cheers,
Aron
回答1:
I believe the answer is to use the the saveToPersistentStore:
method added by the RKAdditions category to NSManagedObjectContext.
This is taken from here and here.
Do you still have issues when using that?
来源:https://stackoverflow.com/questions/14472467/rest-kit-persistance-performance-issue-with-multiplie-mocs