mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 20:31:13 +08:00
Merge branch 'master' of https://github.com/twotoasters/RestKit
This commit is contained in:
@@ -61,10 +61,10 @@ extern NSString* const RKManagedObjectStoreDidFailSaveNotification;
|
||||
/**
|
||||
* Retrieves a model object from the object store given the model object's class and
|
||||
* the primaryKeyValue for the model object. This method leverages techniques specific to
|
||||
* Core Data for optimal performance. The return value will be nil in cases where an existing
|
||||
* object cannot be found in the object store.
|
||||
* Core Data for optimal performance. If an existing object is not found, a new object is created
|
||||
* and returned.
|
||||
*/
|
||||
- (RKManagedObject*)findInstanceOfManagedObject:(Class)class withPrimaryKeyValue:(id)primaryKeyValue;
|
||||
- (RKManagedObject*)findOrCreateInstanceOfManagedObject:(Class)class withPrimaryKeyValue:(id)primaryKeyValue;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@@ -88,6 +88,10 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
|
||||
[managedObjectContext setUndoManager:nil];
|
||||
[managedObjectContext setMergePolicy:NSOverwriteMergePolicy];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(objectsDidChange:)
|
||||
name:NSManagedObjectContextObjectsDidChangeNotification
|
||||
object:managedObjectContext];
|
||||
return managedObjectContext;
|
||||
}
|
||||
|
||||
@@ -159,6 +163,25 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
|
||||
waitUntilDone:YES];
|
||||
}
|
||||
|
||||
- (void)objectsDidChange:(NSNotification*)notification {
|
||||
NSDictionary* userInfo = notification.userInfo;
|
||||
NSSet* insertedObjects = [userInfo objectForKey:NSInsertedObjectsKey];
|
||||
NSMutableDictionary* threadDictionary = [[NSThread currentThread] threadDictionary];
|
||||
|
||||
for (NSManagedObject* object in insertedObjects) {
|
||||
if ([object respondsToSelector:@selector(primaryKeyProperty)]) {
|
||||
Class class = [object class];
|
||||
NSString* primaryKey = [class performSelector:@selector(primaryKeyProperty)];
|
||||
id primaryKeyValue = [object valueForKey:primaryKey];
|
||||
|
||||
NSMutableDictionary* classCache = [threadDictionary objectForKey:class];
|
||||
if (classCache && primaryKeyValue && [classCache objectForKey:primaryKeyValue] == nil) {
|
||||
[classCache setObject:object forKey:primaryKeyValue];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Helpers
|
||||
|
||||
@@ -186,7 +209,7 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
|
||||
return objectArray;
|
||||
}
|
||||
|
||||
- (RKManagedObject*)findInstanceOfManagedObject:(Class)class withPrimaryKeyValue:(id)primaryKeyValue {
|
||||
- (RKManagedObject*)findOrCreateInstanceOfManagedObject:(Class)class withPrimaryKeyValue:(id)primaryKeyValue {
|
||||
RKManagedObject* object = nil;
|
||||
if ([class respondsToSelector:@selector(allObjects)]) {
|
||||
NSArray* objects = nil;
|
||||
@@ -201,7 +224,9 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
|
||||
NSString* primaryKey = [class performSelector:@selector(primaryKeyProperty)];
|
||||
for (id theObject in objects) {
|
||||
id primaryKeyValue = [theObject valueForKey:primaryKey];
|
||||
[dictionary setObject:theObject forKey:primaryKeyValue];
|
||||
if (primaryKeyValue) {
|
||||
[dictionary setObject:theObject forKey:primaryKeyValue];
|
||||
}
|
||||
}
|
||||
|
||||
[threadDictionary setObject:dictionary forKey:class];
|
||||
@@ -209,6 +234,11 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
|
||||
|
||||
NSMutableDictionary* dictionary = [threadDictionary objectForKey:class];
|
||||
object = [dictionary objectForKey:primaryKeyValue];
|
||||
|
||||
if (object == nil && primaryKeyValue && [class respondsToSelector:@selector(object)]) {
|
||||
object = [class object];
|
||||
[dictionary setObject:object forKey:primaryKeyValue];
|
||||
}
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
@@ -58,8 +58,8 @@ extern NSString* const kRKStringBoundary;
|
||||
- (id)initWithName:(NSString*)name file:(NSString*)filePath {
|
||||
if (self = [self initWithName:name]) {
|
||||
NSAssert1([[NSFileManager defaultManager] fileExistsAtPath:filePath], @"Expected file to exist at path: %@", filePath);
|
||||
_fileName = [filePath lastPathComponent];
|
||||
_MIMEType = [self mimeTypeForExtension:[filePath pathExtension]];
|
||||
_fileName = [[filePath lastPathComponent] retain];
|
||||
_MIMEType = [[self mimeTypeForExtension:[filePath pathExtension]] retain];
|
||||
_bodyStream = [[NSInputStream inputStreamWithFileAtPath:filePath] retain];
|
||||
|
||||
NSError* error = nil;
|
||||
@@ -119,8 +119,8 @@ extern NSString* const kRKStringBoundary;
|
||||
} else if (self.MIMEType) {
|
||||
// Typical for data values
|
||||
_MIMEHeader = [[[NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n"
|
||||
@"Content-Type: application/octet-stream\r\n\r\n",
|
||||
[self MIMEBoundary], self.name] dataUsingEncoding:NSUTF8StringEncoding] retain];
|
||||
@"Content-Type: %@\r\n\r\n",
|
||||
[self MIMEBoundary], self.name, self.MIMEType] dataUsingEncoding:NSUTF8StringEncoding] retain];
|
||||
} else {
|
||||
// Typical for raw values
|
||||
_MIMEHeader = [[[NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n",
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
NSObject<RKObjectLoaderDelegate>* _delegate;
|
||||
RKRequest* _request;
|
||||
RKResponse* _response;
|
||||
NSObject<RKObjectMappable>* _source;
|
||||
Class<RKObjectMappable> _objectClass;
|
||||
NSString* _keyPath;
|
||||
RKManagedObjectStore* _managedObjectStore;
|
||||
|
||||
@@ -79,14 +79,6 @@
|
||||
self.request.params = params;
|
||||
}
|
||||
|
||||
- (NSObject<RKObjectMappable>*)source {
|
||||
return (NSObject<RKObjectMappable>*)self.request.userData;
|
||||
}
|
||||
|
||||
- (void)setSource:(NSObject<RKObjectMappable>*)source {
|
||||
self.request.userData = source;
|
||||
}
|
||||
|
||||
- (void)send {
|
||||
[self retain];
|
||||
[self.request send];
|
||||
|
||||
@@ -176,6 +176,11 @@ static RKObjectManager* sharedManager = nil;
|
||||
// Get the serialization representation from the router
|
||||
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;
|
||||
@@ -207,21 +212,18 @@ static RKObjectManager* sharedManager = nil;
|
||||
}
|
||||
|
||||
- (RKObjectLoader*)postObject:(NSObject<RKObjectMappable>*)object delegate:(NSObject<RKObjectLoaderDelegate>*)delegate {
|
||||
[self saveObjectStore];
|
||||
RKObjectLoader* loader = [self objectLoaderForObject:object method:RKRequestMethodPOST delegate:delegate];
|
||||
[loader send];
|
||||
return loader;
|
||||
}
|
||||
|
||||
- (RKObjectLoader*)putObject:(NSObject<RKObjectMappable>*)object delegate:(NSObject<RKObjectLoaderDelegate>*)delegate {
|
||||
[self saveObjectStore];
|
||||
RKObjectLoader* loader = [self objectLoaderForObject:object method:RKRequestMethodPUT delegate:delegate];
|
||||
[loader send];
|
||||
return loader;
|
||||
}
|
||||
|
||||
- (RKObjectLoader*)deleteObject:(NSObject<RKObjectMappable>*)object delegate:(NSObject<RKObjectLoaderDelegate>*)delegate {
|
||||
[self saveObjectStore];
|
||||
RKObjectLoader* loader = [self objectLoaderForObject:object method:RKRequestMethodDELETE delegate:delegate];
|
||||
[loader send];
|
||||
return loader;
|
||||
|
||||
@@ -244,16 +244,12 @@ static const NSString* kRKModelMapperMappingFormatParserKey = @"RKMappingFormatP
|
||||
if ([class respondsToSelector:@selector(primaryKeyElement)]) {
|
||||
NSString* primaryKeyElement = [class performSelector:@selector(primaryKeyElement)];
|
||||
id primaryKeyValue = [elements objectForKey:primaryKeyElement];
|
||||
object = [[[RKObjectManager sharedManager] objectStore] findInstanceOfManagedObject:class
|
||||
object = [[[RKObjectManager sharedManager] objectStore] findOrCreateInstanceOfManagedObject:class
|
||||
withPrimaryKeyValue:primaryKeyValue];
|
||||
}
|
||||
// instantiate if object is nil
|
||||
if (object == nil) {
|
||||
if ([class respondsToSelector:@selector(object)]) {
|
||||
object = [class object];
|
||||
} else {
|
||||
object = [[[class alloc] init] autorelease];
|
||||
}
|
||||
object = [[[class alloc] init] autorelease];
|
||||
}
|
||||
|
||||
return object;
|
||||
@@ -386,7 +382,7 @@ static const NSString* kRKModelMapperMappingFormatParserKey = @"RKMappingFormatP
|
||||
NSDictionary* relationshipsByName = [[managedObject entity] relationshipsByName];
|
||||
NSEntityDescription* relationshipDestinationEntity = [[relationshipsByName objectForKey:relationship] destinationEntity];
|
||||
id relationshipDestinationClass = objc_getClass([[relationshipDestinationEntity managedObjectClassName] cStringUsingEncoding:NSUTF8StringEncoding]);
|
||||
RKManagedObject* relationshipValue = [[[RKObjectManager sharedManager] objectStore] findInstanceOfManagedObject:relationshipDestinationClass
|
||||
RKManagedObject* relationshipValue = [[[RKObjectManager sharedManager] objectStore] findOrCreateInstanceOfManagedObject:relationshipDestinationClass
|
||||
withPrimaryKeyValue:objectPrimaryKeyValue];
|
||||
if (relationshipValue) {
|
||||
[managedObject setValue:relationshipValue forKey:relationship];
|
||||
|
||||
@@ -73,6 +73,11 @@
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params objectClass:(Class)klass delegate:(id)delegate;
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params objectClass:(Class)klass keyPath:(NSString*)keyPath delegate:(id)delegate;
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method delegate:(id)delegate;
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass delegate:(id)delegate;
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass keyPath:(NSString*)keyPath delegate:(id)delegate;
|
||||
|
||||
|
||||
/**
|
||||
* Clear the last loaded time for the model
|
||||
*/
|
||||
|
||||
@@ -76,6 +76,48 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method delegate:(id)delegate {
|
||||
if (self = [self init]) {
|
||||
_resourcePath = [resourcePath retain];
|
||||
_delegate = [delegate retain];
|
||||
_params = [params retain];
|
||||
_method = method;
|
||||
}
|
||||
|
||||
[self loadFromObjectCache];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass delegate:(id)delegate {
|
||||
if (self = [self init]) {
|
||||
_resourcePath = [resourcePath retain];
|
||||
_delegate = [delegate retain];
|
||||
_params = [params retain];
|
||||
_objectClass = [klass retain];
|
||||
_method = method;
|
||||
}
|
||||
|
||||
[self loadFromObjectCache];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass keyPath:(NSString*)keyPath delegate:(id)delegate {
|
||||
if (self = [self init]) {
|
||||
_resourcePath = [resourcePath retain];
|
||||
_delegate = [delegate retain];
|
||||
_params = [params retain];
|
||||
_objectClass = [klass retain];
|
||||
_keyPath = [keyPath retain];
|
||||
_method = method;
|
||||
}
|
||||
|
||||
[self loadFromObjectCache];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// NSObject
|
||||
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params objectClass:(Class)klass;
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params objectClass:(Class)klass keyPath:(NSString*)keyPath;
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method;
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass;
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass keyPath:(NSString*)keyPath;
|
||||
|
||||
- (NSArray*)objects;
|
||||
|
||||
@end
|
||||
|
||||
@@ -40,6 +40,27 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method {
|
||||
if (self = [self init]) {
|
||||
_model = [[RKRequestModel alloc] initWithResourcePath:resourcePath params:params method:method delegate:self];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass {
|
||||
if (self = [self init]) {
|
||||
_model = [[RKRequestModel alloc] initWithResourcePath:resourcePath params:params method:method objectClass:klass delegate:self];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params method:(RKRequestMethod)method objectClass:(Class)klass keyPath:(NSString*)keyPath {
|
||||
if (self = [self init]) {
|
||||
_model = [[RKRequestModel alloc] initWithResourcePath:resourcePath params:params method:method objectClass:klass keyPath:keyPath delegate:self];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// NSObject
|
||||
|
||||
|
||||
Reference in New Issue
Block a user