0
因此,這是我第一次嘗試將對象映射到CoreData中,我認爲它是正確的,但我沒有幾個問題。首先,我只是不知道我是否以正確的方式進行了映射,其次是我得到了成功響應,但沒有映射對象。將一個對象映射到兩個類的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
這是我初始化我CoreData與RestKit:
- (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'?您可以添加兩個響應描述符映射這兩個實體。 – 2014-09-26 06:47:35
@AnushaK是的,我做到了,但仍然無法正常工作。現在我收到了這個錯誤:https://groups.google.com/forum/#!topic/restkit/2u5Ke_FnaZU – 2014-09-26 11:03:27