2012-12-19 78 views
5

假設我要發送一個請求給服務器使用JSONRestKit v0.20.x:同時映射(瞬態)對象和(核心數據)管理對象

{ "begin_session" : { "info" : "this is some info" } } 

,我希望在響應中的JSON :

{ "token" : "this is a token", "a_objects" : [ 
    { "name" : "name of first a_object", "b_objects" : [ 
     { "name" : "name of first b_object", "type" : "some type value", "id" : "123" }, 
     { "name" : "name of second b_object", "type" : "some other type value", "id" : "124" } 
    ], "id" : "id of first a_object" }, 
    { "name" : "name of second a_object", "b_objects" : [ 
     { "name" : "name of first b_object", "type" : "some type value", "id" : "123" }, 
     { "name" : "name of third b_object", "type" : "some third type value" , "id" : "125" }, 
    ], "id" : "id of second a_object" } 
] } 

我想暫時存儲「令牌」並將a_objects保存在覈心數據中。這是我應該怎麼做整個過程?首先,我設置對象:

@interface LoginToken : NSObject 
    @property (nonatomic, copy) NSString *token; 
@end 

@interface AObject : NSManagedObject 
    @property (nonatomic, retain) NSString *name; 
    @property (nonatomic, retain) NSSet *bObjects; 
    @property (nonatomic, retain) NSString *aObjectId; 
@end 

@implementation AObject 
    @dynamic name; @dynamic bObjects; @dynamic aObjectId; 
@end 

@interface BObject : NSManagedObject 
    @property (nonatomic, retain) NSString *name; 
    @property (nonatomic, retain) AObject *aObject; 
    @property (nonatomic, retain) NSString *type; 
    @property (nonatomic, retain) NSString *bObjectId; 
@end 

@implementation BObject 
    @dynamic name; @dynamic aObject; @dynamic type; @dynamic bObjectId; 
@end 

這些是請求參數:

NSDictionary *params = @{"begin_session":@{@"info":@"this is some info"}}; 

然後我設置的映射:

RKObjectMapping *tokenMapping = [RKObjectMapping mappingForClass:[LoginToken class]]; 
[tokenMapping addAttributeMappingsFromArray:@[@"token"]]; 
RKResponseDescriptor *tokenResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:tokenMapping pathPattern:nil keyPath:@"token" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

RKEntityMapping *bObjectMapping = [RKEntityMapping mappingForEntityForName:@"BObject" inManagedObjectStore:objectManager.managedObjectStore]; 
[bObjectMapping addAttributeMappingsFromDictionary:@{@"name":@"name",@"type":@"type", @"id":@"bObjectId"}]; 
bObjectMapping.identificationAttributes = @[@"bObjectId"]; 

RKEntityMapping *aObjectMapping = [RKEntityMapping mappingForEntityForName:@"AObject" inManagedObjectStore:objectManager.managedObjectStore]; 
[aObjectMapping addAttributeMappingsFromDictionary:@{@"name":@"name",@"id":@"aObjectId"}]; 
[aObjectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"b_objects" toKeyPath:@"bObjects" withMapping:bObjectMapping]]; 
aObjectMapping.identificationAttributes = @[@"aObjectId"]; 

假設objectManager是一個正確配置RKObjectManager。我設置了響應描述:

RKResponseDescriptor *tokenResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:tokenMapping pathPattern:nil keyPath:@"token" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

RKResponseDescriptor *aObjectResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:aObjectMapping pathPattern:nil keyPath:@"a_objects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

[objectManager addResponseDescriptorsFromArray:@[tokenResponseDescriptor, aObjectResponseDescriptor]]; 

最後我會做出要求:

[objectManager getObjectsAtPath:@"path" parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
    LoginToken *token = [mappingResult firstObject]; // use this token transiently 
    // coredata objects are auto saved 
} failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    // handle error 
}]; 

請問有什麼需要注意的,如果這是,其實,正確的方法做它?另外,如何設置從BObject到AObject的反向關係......?

回答

0

只要您的CoreData文件具有正確配置的關係(基於您的對象頭文件它看起來是正確的),那麼反轉關係由映射器處理。否則,它應該按原樣工作。

請注意,令牌對象也將保留在CoreData中,因此您可能必須刪除它們,這是所需的行爲。