mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-22 11:46:05 +08:00
introduce RKManagedObjectCache protocol to CoreData component; cleanup remaining NSFetchRequest ivars that are now unused; add RKURL class for making resourcepath and baseurl params available to higher level components; cleanup Three20 library to incorporate latest updates from GateGuru codebase
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#import "RKObjectMapper.h"
|
||||
|
||||
@class RKObjectLoader;
|
||||
@class RKManagedObjectStore;
|
||||
@protocol RKObjectLoaderDelegate <RKRequestDelegate>
|
||||
|
||||
/**
|
||||
@@ -35,8 +36,8 @@
|
||||
RKRequest* _request;
|
||||
RKResponse* _response;
|
||||
Class<RKObjectMappable> _objectClass;
|
||||
NSArray* _fetchRequests;
|
||||
NSString* _keyPath;
|
||||
RKManagedObjectStore* _managedObjectStore;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,20 +90,19 @@
|
||||
*/
|
||||
@property (nonatomic, retain) NSObject<RKRequestSerializable>* params;
|
||||
|
||||
/**
|
||||
* Fetch requests for loading cached objects. This is used to remove objects from the local persistent store
|
||||
* when model mapping operations are completed.
|
||||
*
|
||||
* TODO: May belong in an inherited subclass to isolate persistent/non-persistent mapping in the future.
|
||||
*/
|
||||
@property (nonatomic, retain) NSArray* fetchRequests;
|
||||
|
||||
/*
|
||||
* The keyPath property is an optional property to tell the mapper to map a subset of the response
|
||||
* defined by a specific key.
|
||||
*/
|
||||
@property (nonatomic, copy) NSString* keyPath;
|
||||
|
||||
/*
|
||||
* In cases where CoreData is used for local object storage/caching, a reference to
|
||||
* the managedObjectStore for use in retrieving locally cached objects using the store's
|
||||
* managedObjectCache property.
|
||||
*/
|
||||
@property (nonatomic, retain) RKManagedObjectStore* managedObjectStore;
|
||||
|
||||
/**
|
||||
* Return an auto-released loader with with an object mapper, a request, and a delegate
|
||||
*/
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#import "RKObjectManager.h"
|
||||
#import "Errors.h"
|
||||
#import "RKManagedObject.h"
|
||||
#import "RKURL.h"
|
||||
|
||||
@interface RKObjectLoader (Private)
|
||||
- (void)loadObjectsFromResponse:(RKResponse*)response;
|
||||
@@ -18,9 +19,8 @@
|
||||
|
||||
@implementation RKObjectLoader
|
||||
|
||||
@synthesize mapper = _mapper, delegate = _delegate, fetchRequests = _fetchRequests,
|
||||
request = _request, response = _response, objectClass = _objectClass,
|
||||
source = _source, keyPath = _keyPath;
|
||||
@synthesize mapper = _mapper, delegate = _delegate, request = _request, response = _response,
|
||||
objectClass = _objectClass, source = _source, keyPath = _keyPath, managedObjectStore = _managedObjectStore;
|
||||
|
||||
+ (id)loaderWithMapper:(RKObjectMapper*)mapper request:(RKRequest*)request delegate:(NSObject<RKObjectLoaderDelegate>*)delegate {
|
||||
return [[[self alloc] initWithMapper:mapper request:request delegate:delegate] autorelease];
|
||||
@@ -31,6 +31,7 @@
|
||||
_mapper = [mapper retain];
|
||||
self.request = request;
|
||||
self.delegate = delegate;
|
||||
self.managedObjectStore = nil;
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -41,8 +42,8 @@
|
||||
[_mapper release];
|
||||
[_request release];
|
||||
[_response release];
|
||||
[_fetchRequests release];
|
||||
[_keyPath release];
|
||||
self.managedObjectStore = nil;
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@@ -173,12 +174,16 @@
|
||||
results = [NSArray arrayWithObjects:result, nil];
|
||||
}
|
||||
|
||||
if (self.fetchRequests) {
|
||||
NSArray* cachedObjects = [RKManagedObject objectsWithFetchRequests:self.fetchRequests];
|
||||
for (id object in cachedObjects) {
|
||||
if ([object isKindOfClass:[RKManagedObject class]]) {
|
||||
if (NO == [results containsObject:object]) {
|
||||
[[objectStore managedObjectContext] deleteObject:object];
|
||||
if (self.managedObjectStore && [self.managedObjectStore managedObjectCache]) {
|
||||
if ([self.URL isKindOfClass:[RKURL class]]) {
|
||||
RKURL* rkURL = (RKURL*)self.URL;
|
||||
NSArray* fetchRequests = [[self.managedObjectStore managedObjectCache] fetchRequestsForResourcePath:rkURL.resourcePath];
|
||||
NSArray* cachedObjects = [RKManagedObject objectsWithFetchRequests:fetchRequests];
|
||||
for (id object in cachedObjects) {
|
||||
if ([object isKindOfClass:[RKManagedObject class]]) {
|
||||
if (NO == [results containsObject:object]) {
|
||||
[[objectStore managedObjectContext] deleteObject:object];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,7 +192,7 @@
|
||||
|
||||
// Before looking up NSManagedObjectIDs, need to save to ensure we do not have
|
||||
// temporary IDs for new objects prior to handing the objectIDs across threads
|
||||
NSError* error = [[[RKObjectManager globalManager] objectStore] save];
|
||||
NSError* error = [self.managedObjectStore save];
|
||||
if (nil != error) {
|
||||
NSDictionary* infoDictionary = [[NSDictionary dictionaryWithObjectsAndKeys:response, @"response", error, @"error", nil] retain];
|
||||
[self performSelectorOnMainThread:@selector(informDelegateOfObjectLoadErrorWithInfoDictionary:) withObject:infoDictionary waitUntilDone:NO];
|
||||
|
||||
@@ -171,6 +171,7 @@ static RKObjectManager* globalManager = nil;
|
||||
loader.method = method;
|
||||
loader.params = params;
|
||||
loader.source = object;
|
||||
loader.managedObjectStore = self.objectStore;
|
||||
|
||||
return loader;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user