Ars*_*yev 5 core-data objective-c ios restkit
假设我想用json向服务器发送请求
{ "begin_session" : { "info" : "this is some info" } }
Run Code Online (Sandbox Code Playgroud)
我期待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" }
] }
Run Code Online (Sandbox Code Playgroud)
我想暂时存储"令牌"并将a_object持久存储在核心数据中.这是我应该怎么做的整个过程?首先,我设置了对象:
@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
Run Code Online (Sandbox Code Playgroud)
这些是请求参数:
NSDictionary *params = @{"begin_session":@{@"info":@"this is some info"}};
Run Code Online (Sandbox Code Playgroud)
然后我设置了映射:
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"];
Run Code Online (Sandbox Code Playgroud)
假设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]];
Run Code Online (Sandbox Code Playgroud)
最后我会提出要求:
[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
}];
Run Code Online (Sandbox Code Playgroud)
有没有什么我需要注意的,如果这实际上是正确的方法呢?另外,如何设置从BObject到AObject的反向关系......?
只要您的 CoreData 文件正确配置了关系(根据您的对象头文件,它看起来是正确的),那么映射器就会处理逆关系。否则,它应该按原样工作。
请注意,令牌对象也将保留在 CoreData 中,因此您可能必须删除它们,这是所需的行为。
| 归档时间: |
|
| 查看次数: |
558 次 |
| 最近记录: |