我能够提取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作为具体的子类。

@结束

===============>>#1 票数: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"];

  ask by cipherz translate from so

未解决问题?本站智能推荐: