Eliminate unnecessary manual tracking of inserted objects in favor of MOC insertedObjects:

This commit is contained in:
Blake Watters
2012-08-22 18:54:23 -04:00
parent af36e72ac2
commit 5852d7c997
3 changed files with 22 additions and 64 deletions

View File

@@ -105,7 +105,6 @@
dataSource.operationQueue = [[NSOperationQueue new] autorelease];
[dataSource.operationQueue setSuspended:YES];
[dataSource.operationQueue setMaxConcurrentOperationCount:1];
dataSource.tracksInsertedObjects = YES; // We need to be able to obtain permanent object ID's
self.mappingOperationDataSource = dataSource;
}
@@ -241,27 +240,31 @@
[self deleteCachedObjectsMissingFromResult:result];
__block BOOL success = NO;
__block NSError *error = nil;
NSArray *insertedObjects = [(RKManagedObjectMappingOperationDataSource *)self.mappingOperationDataSource insertedObjects];
RKLogDebug(@"Obtaining permanent object ID's for %ld objects", (unsigned long) [insertedObjects count]);
[self.privateContext performBlockAndWait:^{
success = [self.privateContext obtainPermanentIDsForObjects:insertedObjects error:&error];
}];
if (! success) {
RKLogError(@"Failed to obtain permanent object ID's for %ld managed objects. Error: %@", (unsigned long) [insertedObjects count], [error localizedDescription]);
NSArray *insertedObjects = [[self.privateContext insertedObjects] allObjects];
if ([insertedObjects count] > 0) {
RKLogDebug(@"Obtaining permanent object ID's for %ld objects", (unsigned long) [insertedObjects count]);
[self.privateContext performBlockAndWait:^{
success = [self.privateContext obtainPermanentIDsForObjects:insertedObjects error:&error];
}];
if (! success) {
RKLogError(@"Failed to obtain permanent object ID's for %ld managed objects. Error: %@", (unsigned long) [insertedObjects count], [error localizedDescription]);
}
}
[self.privateContext performBlockAndWait:^{
success = [self.privateContext save:&error];
}];
if (! success) {
RKLogError(@"Failed to save managed object context after mapping completed: %@", [error localizedDescription]);
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:@selector(informDelegateOfError:) withObject:error];
[self finalizeLoad:success];
});
return;
if ([self.privateContext hasChanges]) {
[self.privateContext performBlockAndWait:^{
success = [self.privateContext save:&error];
}];
if (! success) {
RKLogError(@"Failed to save managed object context after mapping completed: %@", [error localizedDescription]);
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:@selector(informDelegateOfError:) withObject:error];
[self finalizeLoad:success];
});
return;
}
}
}

View File

@@ -43,31 +43,6 @@
*/
@property (nonatomic, retain) NSOperationQueue *operationQueue;
/**
A Boolean value determing if the receiver keeps tracks of inserted objects.
Tracking inserted objects is useful in mapping operations that are performed on a background thread
but intend to return results to another thread. In these cases, it is necessary to obtain permanent object
ID's for all inserted objects so that they may be reliably fetched across NSManagedObjectContext instances.
Managed objects inserted into a context that is not directly related to the persistent store coordinator (i.e.
child managed object contexts) do not have their managed object ID's updtated from temporary to persistent when
saved to the persistent store through a parent context.
**Default**: NO
*/
@property (nonatomic, assign) BOOL tracksInsertedObjects;
/**
Returns the list of managed objects inserted into the receiver's managed object context during the course of
a managed object mapping operation.
*/
@property (nonatomic, readonly) NSArray *insertedObjects;
/**
Clears the list of inserted objects tracked by the receiver.
*/
- (void)clearInsertedObjects;
/**
Initializes the receiver with a given managed object context and managed object cache.

View File

@@ -19,7 +19,6 @@
@interface RKManagedObjectMappingOperationDataSource ()
@property (nonatomic, retain, readwrite) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readwrite) id<RKManagedObjectCaching> managedObjectCache;
@property (nonatomic, retain) NSMutableArray *mutableInsertedObjects;
@end
@implementation RKManagedObjectMappingOperationDataSource
@@ -27,8 +26,6 @@
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectCache = _managedObjectCache;
@synthesize operationQueue = _operationQueue;
@synthesize tracksInsertedObjects = _tracksInsertedObjects;
@synthesize mutableInsertedObjects = _mutableInsertedObjects;
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext cache:(id<RKManagedObjectCaching>)managedObjectCache
{
@@ -38,8 +35,6 @@
if (self) {
self.managedObjectContext = managedObjectContext;
self.managedObjectCache = managedObjectCache;
self.mutableInsertedObjects = [NSMutableArray array];
self.tracksInsertedObjects = NO;
}
return self;
@@ -50,7 +45,6 @@
[_managedObjectCache release];
[_managedObjectContext release];
[_operationQueue release];
[_mutableInsertedObjects release];
[super dealloc];
}
@@ -124,10 +118,6 @@
if ([self.managedObjectCache respondsToSelector:@selector(didCreateObject:)]) {
[self.managedObjectCache didCreateObject:object];
}
if (self.tracksInsertedObjects) {
[self.mutableInsertedObjects addObject:object];
}
}
return object;
@@ -170,14 +160,4 @@
}
}
- (NSArray *)insertedObjects
{
return [NSArray arrayWithArray:self.mutableInsertedObjects];
}
- (void)clearInsertedObjects
{
self.mutableInsertedObjects = [NSMutableArray array];
}
@end