使用CoreData时,RestKit无法正确映射属性

时间:2012-07-03 00:24:47

标签: iphone ios web-services core-data restkit

我在使用我的应用程序实现RestKit / CoreData时遇到了一些问题。使用RKTwitterCoreData示例应用程序并将其指向我的json feed的Web服务,我得到了生成的tableview:

Duplicate objects/ID not being displayed correctly

到目前为止遇到的问题:

  1. 刷新生成新的核心数据条目,即使我已将primaryKeyAttribute设置为orderID。
  2. cell textLabel显示与我的网络服务
  3. 不匹配的奇怪订单ID

    这是我的applicationDidFinishLaunchingWithOptions:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Initialize RestKit
    RKObjectManager* objectManager = [RKObjectManager managerWithBaseURLString:@"http://mywebservice.com"];
    
    // Enable automatic network activity indicator management
    objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
    
    NSString *databaseName = @"RKTwitterData.sqlite";
    objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName usingSeedDatabaseName:nil managedObjectModel:nil delegate:self];
    
    RKManagedObjectMapping* statusMapping = [RKManagedObjectMapping mappingForClass:[Order class] inManagedObjectStore:objectManager.objectStore];
    statusMapping.primaryKeyAttribute = @"orderID";
    [statusMapping mapKeyPath:@"id" toAttribute:@"orderID"];
    [statusMapping mapAttributes:@"created_at", nil];
    
    // Register our mappings with the provider
    [objectManager.mappingProvider setObjectMapping:orderMapping forResourcePathPattern:@"/orders"];
    
    // Create Window and View Controllers
    RKTwitterViewController* viewController = [[[RKTwitterViewController alloc] initWithNibName:nil bundle:nil] autorelease];
    UINavigationController* controller = [[UINavigationController alloc] initWithRootViewController:viewController];
    UIWindow* window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [window addSubview:controller.view];
    [window makeKeyAndVisible];
    
    return YES;
    }
    

    json结果为/ orders:

    enter image description here

    处理此问题的任何建议?谢谢!

1 个答案:

答案 0 :(得分:0)

卫生署!原因是因为twitter API不符合键值编码。一旦我改变了:

 [objectManager.mappingProvider setObjectMapping:orderMapping forResourcePathPattern:@"/orders"];

使用:

 [objectManager.mappingProvider setObjectMapping:orderMapping forKeyPath:@"orders"];

又恢复了工作!

谢谢并希望它有所帮助。