使用RestKit将NSManagedObject实体映射到自定义类

时间:2011-12-11 16:46:45

标签: json core-data restkit

我能够提取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类映射到数据模型中的Entity,这样就可以正常工作。最终,我试图将我的Customer类用作MKAnnotation,但我不能,因为我无法提供具体的实现类。我确信存在某种对RestKit的误解。我看到RKTwitterCoreData将RKStatus作为具体的子类。

@end

1 个答案:

答案 0 :(得分:0)

确定,    经过多次试验和错误,我想出来了!问题是我需要在我的映射上设置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"];