我正在使用RESTKIT 0.20,我遇到了映射问题。谁能告诉我哪里出错?谢谢,还有我的代码:
RKObjectMapping* pageMapping = [RKEntityMapping mappingForEntityForName:@"Page" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[pageMapping addAttributeMappingsFromArray:@[ @"rows", @"columns" ]];
RKObjectMapping* magazineMapping = [RKEntityMapping mappingForEntityForName:@"Magazine" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[magazineMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"pages"
toKeyPath:@"pages"
withMapping:pageMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:magazineMapping pathPattern:nil keyPath:@"magazine" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kBasePath, [magazine contentPath]]];
NSURLRequest *r = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:r responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
有实体杂志:
@interface Magazine : NSManagedObject
@property (strong) NSString * contentPath;
@property (strong) NSDate * date;
@property (strong) NSString * icon;
@property (strong) NSString * magazineID;
@property (strong) NSDecimalNumber * price;
@property (strong) id priceLocale;
@property (strong) NSString * title;
@property (strong) Page *pages;
最后是Page实体:
@class Magazine;
@interface Page : NSManagedObject
@property (strong) NSString * content;
@property (strong) NSNumber * contentID;
@property (strong) NSNumber * columns;
@property (strong) NSNumber * rows;
@property (strong) NSString *pageID;
@property (strong) Magazine *assignedMagazine;
错误必须在映射页面实体中,因为其他“下载”方法没有问题。
答案 0 :(得分:1)
使用这样的映射:
RKManagedObjectMapping *mappingPage = [RKManagedObjectMapping mappingForClass:[Page class] inManagedObjectStore:self.objectStore];
mappingPage.setDefaultValueForMissingAttributes = NO;
mappingPage.primaryKeyAttribute = @"contentID";
[mappingPage mapKeyPathsToAttributes:
@"CONTENT_FROM_WEBSERVICE", @"content",
@"ID_FROM_WEBSERVICE", @"contentID",
@"COLUMNS_FROM_WEBSERVICE", @"columns",
@"ROWS_FROM_WEBSERVICE", @"rows",
nil];