Add truncation example code to the resetPersistentStores block

This commit is contained in:
Blake Watters
2012-12-20 18:59:14 -05:00
parent ef038aed73
commit fccfd7724f

View File

@@ -38,9 +38,6 @@
The managed object context hierarchy is designed to isolate the main thread from disk I/O and avoid deadlocks. Because the primary context manages its own private queue, saving the main queue context will not result in the objects being saved to the persistent store. The primary context must be saved as well for objects to be persisted to disk.
It is also worth noting that because of the parent/child context hierarchy, objects created on the main thread will not obtain permanent managed object ID's even after the primary context has been saved. If you need to refer to the permanent representations of objects created on the main thread after a save, you may ask the main queue context to obtain permanent managed objects for your objects via `obtainPermanentIDsForObjects:error:`. Be warned that when obtaining permanent managed object ID's, you must include all newly created objects that are reachable from the object you are concerned with in the set of objects provided to `obtainPermanentIDsForObjects:error:`. This means any newly created object in a one-to-one or one-to-many relationship must be provided or you will face a crash from the managed object context. This is due to a bug in Core Data still present in iOS5, but fixed in iOS6 (see Open Radar http://openradar.appspot.com/11478919).
@see `NSManagedObjectContext (RKAdditions)`
@see `NSEntityDescription (RKAdditions)`
*/
@interface RKManagedObjectStore : NSObject
@@ -151,6 +148,32 @@
@bug This method will implictly result in the managed object contexts associated with the receiver to be discarded and recreated. Any managed objects or additional child contexts associated with the store will need to be discarded or else exceptions may be raised (i.e. `NSObjectInaccessibleException`).
Also note that care must be taken to cancel/suspend all mapping operations, reset all managed object contexts, and disconnect all `NSFetchedResultController` objects that are associated with managed object contexts using the persistent stores of the receiver before attempting a reset. Failure to completely disconnect usage before calling this method is likely to result in a deadlock.
As an alternative to resetting the persistent store, you may wish to consider simply deleting all managed objects out of the managed object context. If your data set is not very large, this can be a performant operation and is significantly easier to implement correctly. An example implementation for truncating all managed objects from the store is provided below:
NSBlockOpertation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSManagedObjectContext *managedObjectContext = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;
[managedObjectContext performBlockAndWait:^{
NSError *error = nil;
for (NSEntityDescription *entity in [RKManagedObjectStore defaultStore].managedObjectModel) {
NSFetchRequest *fetchRequest = [NSFetchRequest new];
[fetchRequest setEntity:entity];
[fetchRequest setIncludesSubentities:NO];
NSArray *objects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (! objects) RKLogWarning(@"Failed execution of fetch request %@: %@", fetchRequest, error);
for (NSManagedObject *managedObject in objects) {
[managedObjectContext deleteObject:managedObjectContext];
}
}
BOOL success = [managedObjectContext save:&error];
if (!success) RKLogWarning(@"Failed saving managed object context: %@", error);
}];
}];
[operation setCompletionBlock:^{
// Do stuff once the truncation is complete
}];
[operation start];
*/
- (BOOL)resetPersistentStores:(NSError **)error;