【问题标题】:RestKit not mapping attributes properly when using CoreData使用 CoreData 时 RestKit 没有正确映射属性
【发布时间】:2012-07-03 00:24:46
【问题描述】:

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

目前遇到的问题:

  1. 即使我将 primaryKeyAttribute 设置为 orderID,Refresh 也会生成一个新的核心数据条目。
  2. cell textLabel 显示一个奇怪的 orderID,与我的 Web 服务不匹配

这是我的 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;
}

以及 /orders 处的 json 结果:

对于处理这个问题有什么建议吗?谢谢!

【问题讨论】:

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


    【解决方案1】:

    哇!原因是 twitter API 不符合键值编码。一旦我改变:

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

    与:

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

    又成功了!

    谢谢,希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多