使用 2 个类将对象映射到 CoreData
Posted
技术标签:
【中文标题】使用 2 个类将对象映射到 CoreData【英文标题】:Mapping an object to CoreData with 2 classes 【发布时间】:2014-09-25 21:08:12 【问题描述】:所以这是我第一次尝试将对象映射到 CoreData,我认为我做对了,但我遇到的问题很少。首先,我只是不知道我是否以正确的方式映射它,第二个是我得到了成功响应但没有映射对象。
我有 2 个对象 DbRestError 和 DbRestCategory。 DbRestError 包含来自服务器的错误,而 DbRestCategory 包含类别本身。两者都从相同的响应中返回,但 RestKit 似乎无法映射它们。
这些是对象:
DbRestError
@interface DbRestError : NSManagedObject
@property (nonatomic, retain) NSNumber * statusCode;
@property (nonatomic, retain) NSString * errorTitle;
@end
DbRestCategory
@interface DbRestCategory : NSManagedObject
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSNumber * idNum;
@end
这就是我使用 RestKit 初始化 CoreData 的方式:
- (void)setup
self.objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"database.sqlite"];
NSLog(@"Setting up store at %@", path);
[self.objectStore addSQLitePersistentStoreAtPath:path
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:@ NSInferMappingModelAutomaticallyOption: @YES, NSMigratePersistentStoresAutomaticallyOption: @YES
error:nil];
[self.objectStore createManagedObjectContexts];
这就是我初始化我的 RestKit 客户端的方式:
- (void)initRestClient
NSURL *baseURL = [NSURL URLWithString:kWebServiceBaseURL];
self.manager = [RKObjectManager managerWithBaseURL:baseURL];
[self.manager setRequestSerializationMIMEType:RKMIMETypeJSON];
[self.manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[self.manager addResponseDescriptorsFromArray:[RKObjectManager sharedManager].responseDescriptors];
[self.manager addRequestDescriptorsFromArray:[RKObjectManager sharedManager].requestDescriptors];
[self.manager.HTTPClient.operationQueue setMaxConcurrentOperationCount:MAX_CONCURRENT_OPERATION_COUNT];
[self.manager setManagedObjectStore:[[MainDb sharedDb] objectStore]];
[RKObjectManager setSharedManager:self.manager];
self.requestMethod = RKRequestMethodGET;
self.statusCode = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKRoute *categoriesRoute = [RKRoute routeWithClass:[DbRestCategory class] pathPattern:kGetCategories method:self.requestMethod];
categoriesRoute.shouldEscapePath = YES;
[self.manager.router.routeSet addRoute:categoriesRoute];
// @@yosi -- Error Mapping
//
//
RKEntityMapping *errorMapping = [RKEntityMapping mappingForEntityForName:@"DbRestError" inManagedObjectStore:[[MainDb sharedDb] objectStore]];
[errorMapping addAttributeMappingsFromDictionary:@ @"statusCode" : @"statusCode", @"description" : @"errorTitle" ];
// @@yosi -- Categories Mapping
//
//
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"DbRestCategory" inManagedObjectStore:[[MainDb sharedDb] objectStore]];
[categoryMapping addAttributeMappingsFromDictionary:@ @"id" : @"idNum", @"name" : @"title" ];
RKObjectMapping *categoriesMapping = [RKObjectMapping mappingForClass:[RestCategories class]];
[categoriesMapping addRelationshipMappingWithSourceKeyPath:@"categories" mapping:categoryMapping];
[categoriesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"error" toKeyPath:@"error" withMapping:errorMapping]];
[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:categoriesMapping
method:self.requestMethod
pathPattern:kGetCategories
keyPath:nil
statusCodes:self.statusCode]];
这就是我自己处理请求的方式:
[[RKObjectManager sharedManager] getObject:[[DbRestCategory alloc] init] path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
failure:^(RKObjectRequestOperation *operation, NSError *error)
];
这是 JSON 的样子:
error:
statusCode: 10,
description: "success"
,
categories: [
id: "3",
name: "cate 3"
,
id: "4",
name: "cate 4"
,
id: "5",
name: "cate 5"
,
id: "1",
name: "cate 1"
,
id: "6",
name: "cate 6"
,
id: "2",
name: "cate 2"
]
有人可以帮我理解为什么我得到一个没有任何对象的响应吗?
【问题讨论】:
error
和 categories
在相同的响应下。你为什么使用方法getObject
而不是getObjectsAtPath
?您可以添加两个响应描述符映射两个实体。
@AnushaK 是的,我做到了,但仍然无法正常工作。现在我收到此错误:groups.google.com/forum/#!topic/restkit/2u5Ke_FnaZU
【参考方案1】:
试试这样的请求映射,
// Request Mapping
// Map error
RKEntityMapping *errorMapping = [RKEntityMapping mappingForEntityForName:@"DbRestError" inManagedObjectStore:[[MainDb sharedDb] objectStore]];
[errorMapping addAttributeMappingsFromDictionary:@ @"statusCode" : @"statusCode", @"description" : @"errorTitle" ];
[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:self.requestMethod pathPattern:kGetCategories keyPath:@"error" statusCodes:self.statusCode]];
// Map Category
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"DbRestCategory" inManagedObjectStore:[[MainDb sharedDb] objectStore]];
[categoryMapping addAttributeMappingsFromDictionary:@ @"id" : @"idNum", @"name" : @"title" ];
[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:categoriesMapping method:self.requestMethod pathPattern:kGetCategories keyPath:@"categories" statusCodes:self.statusCode]];
【讨论】:
以上是关于使用 2 个类将对象映射到 CoreData的主要内容,如果未能解决你的问题,请参考以下文章