mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-01 22:42:51 +08:00
* Introduces RKRequestCache for cacheing responses (supports ETag conditional GET, use cache if available, use cache on error, etc.) closes #75 * Updates to Three20 layer to eliminate need for intermediary TTTableItem classes closes #76 * Fixes to ensure iOS 3.x compatability: * Switched compiler to Clang * Updated conditional checks for UIBackgroundTask symbols to ensure runtime safety on iOS 3.x * Removed unnecessary linkage against UIKit and CoreFoundation from library targets * Fix for issue where RKRequest objects could become stuck in infinite loop within RKRequestQueue loadNextInQueue if you start a request and then cancel immediately. On cancel only decrement loadCount if the request has start loading. refs #122
120 lines
4.0 KiB
Objective-C
120 lines
4.0 KiB
Objective-C
//
|
|
// RKManagedObjectLoader.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 2/13/11.
|
|
// Copyright 2011 Two Toasters. All rights reserved.
|
|
//
|
|
|
|
#import "RKObjectManager.h"
|
|
#import "RKManagedObjectLoader.h"
|
|
#import "RKURL.h"
|
|
#import "RKObjectMapper.h"
|
|
#import "RKManagedObjectFactory.h"
|
|
#import "RKManagedObjectThreadSafeInvocation.h"
|
|
#import "../ObjectMapping/RKObjectLoader_Internals.h"
|
|
#import "../Network/RKRequest_Internals.h"
|
|
|
|
@implementation RKManagedObjectLoader
|
|
|
|
- (id)init {
|
|
self = [super init];
|
|
if (self) {
|
|
_managedObjectKeyPaths = [[NSMutableSet alloc] init];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
[_targetObjectID release];
|
|
_targetObjectID = nil;
|
|
[_managedObjectKeyPaths release];
|
|
|
|
[super dealloc];
|
|
}
|
|
|
|
- (RKManagedObjectStore*)objectStore {
|
|
return self.objectManager.objectStore;
|
|
}
|
|
|
|
#pragma mark - RKObjectMapperDelegate methods
|
|
|
|
- (void)objectMapper:(RKObjectMapper*)objectMapper didMapFromObject:(id)sourceObject toObject:(id)destinationObject atKeyPath:(NSString*)keyPath usingMapping:(RKObjectMapping*)objectMapping {
|
|
if ([destinationObject isKindOfClass:[NSManagedObject class]]) {
|
|
[_managedObjectKeyPaths addObject:keyPath];
|
|
}
|
|
}
|
|
|
|
#pragma mark - RKObjectLoader overrides
|
|
|
|
// Overload the target object reader to return a thread-local copy of the target object
|
|
- (id)targetObject {
|
|
if ([NSThread isMainThread] == NO && _targetObjectID) {
|
|
return [self.objectStore objectWithID:_targetObjectID];
|
|
}
|
|
|
|
return _targetObject;
|
|
}
|
|
|
|
- (void)setTargetObject:(NSObject*)targetObject {
|
|
[_targetObject release];
|
|
_targetObject = nil;
|
|
_targetObject = [targetObject retain];
|
|
|
|
[_targetObjectID release];
|
|
_targetObjectID = nil;
|
|
}
|
|
|
|
- (void)prepareURLRequest {
|
|
// TODO: Can we just do this if the object hasn't been saved already???
|
|
|
|
// 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.
|
|
if (self.targetObject && [self.targetObject isKindOfClass:[NSManagedObject class]]) {
|
|
[self.objectStore save];
|
|
_targetObjectID = [[(NSManagedObject*)self.targetObject objectID] retain];
|
|
}
|
|
|
|
[super prepareURLRequest];
|
|
}
|
|
|
|
// NOTE: We are on the background thread here, be mindful of Core Data's threading needs
|
|
- (void)processMappingResult:(RKObjectMappingResult*)result {
|
|
if (_targetObjectID && self.targetObject && self.method == RKRequestMethodDELETE) {
|
|
// TODO: Logging
|
|
NSManagedObject* backgroundThreadObject = [self.objectStore objectWithID:_targetObjectID];
|
|
[[self.objectStore managedObjectContext] deleteObject:backgroundThreadObject];
|
|
}
|
|
|
|
// If the response was successful, save the store...
|
|
if ([self.response isSuccessful]) {
|
|
// TODO: Logging or delegate notifications?
|
|
[self.objectStore save];
|
|
}
|
|
|
|
// TODO: If unsuccessful and we saved the object, remove it from the store so that it is not orphaned
|
|
|
|
NSDictionary* dictionary = [result asDictionary];
|
|
NSMethodSignature* signature = [self methodSignatureForSelector:@selector(informDelegateOfObjectLoadWithResultDictionary:)];
|
|
RKManagedObjectThreadSafeInvocation* invocation = [RKManagedObjectThreadSafeInvocation invocationWithMethodSignature:signature];
|
|
[invocation setObjectStore:self.objectStore];
|
|
[invocation setTarget:self];
|
|
[invocation setSelector:@selector(informDelegateOfObjectLoadWithResultDictionary:)];
|
|
[invocation setArgument:&dictionary atIndex:2];
|
|
[invocation setManagedObjectKeyPaths:_managedObjectKeyPaths forArgument:2];
|
|
[invocation invokeOnMainThread];
|
|
}
|
|
|
|
- (id<RKObjectFactory>)createObjectFactory {
|
|
if (self.objectManager.objectStore) {
|
|
return [RKManagedObjectFactory objectFactoryWithObjectStore:self.objectStore];
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
@end
|