使用 RestKit 将 NSManagedObject 实体映射到自定义类
Posted
技术标签:
【中文标题】使用 RestKit 将 NSManagedObject 实体映射到自定义类【英文标题】:Mapping NSManagedObject Entity to custom class with RestKit 【发布时间】:2011-12-11 16:46:45 【问题描述】:我能够提取 JSON 结果并将它们映射到数据库。但是,一旦我尝试将实体映射到我的子类,我的映射似乎就中断了。
JSON Sample:
"Customers":["Customer":"Active":"Y","BranchID":0,"CustomerID":"SMCJIMB00736253","CustomerIdentifier":"","CustomerTypeID":0,"Name":""]
Setup:
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:TEST_SERVICES_ROOT];
// Initialize object store
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:RKDefaultSeedDatabaseFileName];
Mapping:
RKManagedObjectMapping* customerMapping = [RKManagedObjectMapping mappingForClass:[Customer class]];
customerMapping.primaryKeyAttribute = @"customerID";
[customerMapping mapKeyPathsToAttributes:@"Active", @"active", nil];
[customerMapping mapKeyPathsToAttributes:@"BranchID", @"branchID", nil];
[customerMapping mapKeyPathsToAttributes:@"CustomerID", @"customerID", nil];
[customerMapping mapKeyPathsToAttributes:@"CustomerIdentifier", @"customerIdentifier", nil];
[customerMapping mapKeyPathsToAttributes:@"CustomerTypeID", @"customerTypeID", nil];
[customerMapping mapKeyPathsToAttributes:@"Name", @"name", nil];
Add Mapping:
[objectManager.mappingProvider setMapping:customerMapping forKeyPath:@"Customers.Customer"];
Call WS:
// Load the object model via RestKit
RKObjectManager* objectManager = [RKObjectManager sharedManager];
NSString *url = [NSString stringWithFormat:SERVICE_CUSTOMERS,@"0",@"1",@"100"];
[objectManager loadObjectsAtResourcePath:url delegate:self block:^(RKObjectLoader* loader)
loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[Customer class]];
];
@interface Customer : NSManagedObject
@property (nonatomic, retain) NSString* active;
@property (nonatomic, retain) NSNumber* branchID;
@property (nonatomic, retain) NSString* customerID;
@property (nonatomic, retain) NSString* customerIdentifier;
@property (nonatomic, retain) NSNumber* customerTypeID;
@property (nonatomic, retain) NSString* name;
//populated via fetch predicate
@property (nonatomic, retain) NSArray* customerAddress;
@end
@implementation Customer
@dynamic active;
@dynamic branchID;
@dynamic customerID;
@dynamic customerIdentifier;
@dynamic customerTypeID;
@dynamic name;
//populated via fetch predicate
@dynamic customerAddress;
@end
只要我不尝试将 Customer 类映射到数据模型中的实体,它就可以正常工作。最终,我试图将我的 Customer 类用作 MKAnnotation,但我不能,因为我无法提供具体的实现类。我敢肯定对 RestKit 存在某种误解。我看到 RKTwitterCoreData 将 RKStatus 作为具体的子类。
@结束
【问题讨论】:
【参考方案1】:好的, 经过多次试验和错误,我想通了!问题是我需要在我的映射上设置我的 rootKeyPath 以匹配我在将映射添加到映射提供程序时使用的 rootkeyPath。 RKTwitterCoreData 客户端不这样做,但无论出于何种原因我都需要这样做。
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]];
userMapping.rootKeyPath = @"User";
userMapping.primaryKeyAttribute = @"userName";
[userMapping mapKeyPathsToAttributes:@"Authenticated", @"authenticated", nil];
................
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"User"];
【讨论】:
以上是关于使用 RestKit 将 NSManagedObject 实体映射到自定义类的主要内容,如果未能解决你的问题,请参考以下文章
将“import <RestKit/RestKit.h>”添加到 AppDelegate.m 的问题
如何使用 RestKit 将日期组件字典映射到 NSDate
使用 RestKit 将 JSON“关联数组”映射到 CoreData