2012-12-19 13 views
5

Ben jsonRestKit v0.20.x: Aynı anda haritalama (geçici) nesnesi ve (çekirdek verileri) yönetilen nesne

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

ile sunucuya bir istek göndermek istiyorum ve tepki json bekliyoruz varsayalım :

{ "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" } 
] } 

I geçici "belirteç" depolamak ve çekirdek veri a_objects devam etmek istiyorum. Tüm süreci nasıl yapmalıyım?

@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 

Bunlar istek parametreleri şunlardır:

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

Sonra eşlemeler kurmak:

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"]; 

varsayalım objectManager doğru yapılandırılmış RKObjectManager olduğu Birincisi, nesneleri kurdu. Ben tepki tanımlayıcılar kurmak:

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]]; 

Ve sonunda isteği yapacağız:

[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 
}]; 

aslında bu ise farkında olmak gerekir bir şey var mı, doğru yolu yap? Ayrıca, ters ilişkiyi BObject'ten AObject'e nasıl ayarlayabilirim?

cevap

0

CoreData dosyanız, doğru yapılandırılmış ilişkiye sahip olduğu sürece (nesne başlık dosyalarınıza göre doğru göründüğü), ters ilişki eşleştirici tarafından işlenir. Aksi halde olduğu gibi çalışması gerekir.

Simge nesnesinin CoreData'da da kalıcı olacağını ve bunları istediğiniz davranışı silmek zorunda kalabileceğinizi unutmayın.