1

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

重复的对象/ID 未正确显示

目前遇到的问题:

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

这是我的 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 结果:

在此处输入图像描述

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

4

1 回答 1

0

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

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

和:

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

它再次起作用了!

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

于 2012-07-03T01:02:11.360 回答