Removed saveObjectStore method on RKObjectManager, step toward separating Core Data from the base object mapping. RKObjectLoader now ensures that the object store is persisted appropriately before object loaders are sent

This commit is contained in:
Blake Watters
2011-01-21 00:56:17 -05:00
parent 8cef9936b3
commit 39a77057ed
7 changed files with 24 additions and 36 deletions

View File

@@ -28,7 +28,7 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
- (id)initWithStoreFilename:(NSString*)storeFilename {
if (self = [self init]) {
_storeFilename = [storeFilename retain];
_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
[self createPersistentStoreCoordinator];
}

View File

@@ -13,6 +13,7 @@
@class RKObjectLoader;
@class RKManagedObjectStore;
@protocol RKObjectLoaderDelegate <RKRequestDelegate>
/**

View File

@@ -63,15 +63,11 @@
- (void)setTargetObject:(NSObject<RKObjectMappable>*)targetObject {
[_targetObject release];
_targetObject = nil;
_targetObject = [targetObject retain];
_targetObject = nil;
_targetObject = [targetObject retain];
[_targetObjectID release];
_targetObjectID = nil;
if ([targetObject isKindOfClass:[NSManagedObject class]]) {
_targetObjectID = [[(NSManagedObject*)targetObject objectID] retain];
}
}
@@ -276,7 +272,7 @@
}
if (NO == [self encounteredErrorWhileProcessingRequest:response]) {
// TODO: When other mapping formats are supported, unwind this assumption...
// TODO: When other mapping formats are supported, unwind this assumption... Should probably be an expected MIME types array set by client/manager
if ([response isSuccessful] && [response isJSON]) {
[self performSelectorInBackground:@selector(processLoadModelsInBackground:) withObject:response];
} else {
@@ -290,8 +286,18 @@
}
// Give the target object a chance to modify the request
- (void)triggerWillSendForTargetObject {
- (void)handleTargetObject {
if (self.targetObject) {
if ([self.targetObject isKindOfClass:[NSManagedObject class]]) {
// NOTE: There is an important sequencing issue here. You MUST save the
// managed object context before retaining the objectID or you will run
// into an error where the object context cannot be saved. We do this
// right before send to avoid sequencing issues where the target object is
// set before the managed object store.
[self.managedObjectStore save];
_targetObjectID = [[(NSManagedObject*)self.targetObject objectID] retain];
}
if ([self.targetObject respondsToSelector:@selector(willSendWithObjectLoader:)]) {
[self.targetObject willSendWithObjectLoader:self];
}
@@ -299,12 +305,12 @@
}
- (void)send {
[self triggerWillSendForTargetObject];
[self handleTargetObject];
[super send];
}
- (RKResponse*)sendSynchronously {
[self triggerWillSendForTargetObject];
[self handleTargetObject];
return [super sendSynchronously];
}

View File

@@ -196,7 +196,4 @@ typedef enum {
*/
- (RKObjectLoader*)objectLoaderForObject:(NSObject<RKObjectMappable>*)object method:(RKRequestMethod)method delegate:(NSObject<RKObjectLoaderDelegate>*)delegate;
// Saves the object store. Temporarily exposed here until Core Data is cleaned up.
- (void)saveObjectStore;
@end

View File

@@ -179,10 +179,6 @@ static RKObjectManager* sharedManager = nil;
NSString* resourcePath = [self.router resourcePathForObject:object method:method];
NSObject<RKRequestSerializable>* params = [self.router serializationForObject:object method:method];
if (method != RKRequestMethodGET) {
[self saveObjectStore];
}
RKObjectLoader* loader = [self objectLoaderWithResourcePath:resourcePath delegate:delegate];
loader.method = method;
@@ -194,16 +190,6 @@ static RKObjectManager* sharedManager = nil;
return loader;
}
// TODO: Need to factor core data stuff out of here... Use notifications (probably at RKObjectLoader level) to trigger save of the object store
- (void)saveObjectStore {
if (self.objectStore) {
NSError* error = [self.objectStore save];
if (nil != error) {
NSLog(@"[RestKit] RKObjectManager: Error saving managed object context before PUT/POST/DELETE: error=%@ userInfo=%@", error, error.userInfo);
}
}
}
- (RKObjectLoader*)getObject:(NSObject<RKObjectMappable>*)object delegate:(NSObject<RKObjectLoaderDelegate>*)delegate {
RKObjectLoader* loader = [self objectLoaderForObject:object method:RKRequestMethodGET delegate:delegate];
[loader send];

View File

@@ -98,13 +98,11 @@ static DBUser* currentUser = nil;
- (void)loginWithUsername:(NSString*)username andPassword:(NSString*)password delegate:(NSObject<DBUserAuthenticationDelegate>*)delegate {
_delegate = delegate;
// TODO: Cleanup. Save the object store so that background threads can update this object
[[RKObjectManager sharedManager] saveObjectStore];
RKObjectLoader* objectLoader = [[RKObjectManager sharedManager] objectLoaderWithResourcePath:@"/login" delegate:self];
objectLoader.method = RKRequestMethodPOST;
objectLoader.params = [NSDictionary dictionaryWithKeysAndObjects:@"user[username]", username, @"user[password]", password, nil];
objectLoader.targetObject = self;
objectLoader.params = [NSDictionary dictionaryWithKeysAndObjects:@"user[username]", username, @"user[password]", password, nil];
objectLoader.targetObject = self;
objectLoader.managedObjectStore = [RKObjectManager sharedManager].objectStore;
[objectLoader send];
}

View File

@@ -147,7 +147,7 @@
/* Begin PBXFileReference section */
1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1D6058910D05DD3D006BFB54 /* DiscussionBoard.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DiscussionBoard.app; sourceTree = BUILT_PRODUCTS_DIR; };
1D6058910D05DD3D006BFB54 /* DiscussionBoard-Development.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "DiscussionBoard-Development.app"; sourceTree = BUILT_PRODUCTS_DIR; };
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
25359D4212E3E0A4008BF33D /* libThree20.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libThree20.a; sourceTree = "<group>"; };
25359D4312E3E0A4008BF33D /* libThree20Core.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libThree20Core.a; sourceTree = "<group>"; };
@@ -537,7 +537,7 @@
19C28FACFE9D520D11CA2CBB /* Products */ = {
isa = PBXGroup;
children = (
1D6058910D05DD3D006BFB54 /* DiscussionBoard.app */,
1D6058910D05DD3D006BFB54 /* DiscussionBoard-Development.app */,
);
name = Products;
sourceTree = "<group>";
@@ -1108,7 +1108,7 @@
);
name = DiscussionBoard;
productName = DiscussionBoard;
productReference = 1D6058910D05DD3D006BFB54 /* DiscussionBoard.app */;
productReference = 1D6058910D05DD3D006BFB54 /* DiscussionBoard-Development.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */