CoreData + RestKit保留本地实体

时间:2012-07-13 07:52:14

标签: objective-c core-data synchronization restkit

我正在编写一个使用CoreData + RestKit的iOS应用程序。我有一个问题,当我从服务器同步时,本地未同步的实体无效。我正在使用RestKit 0.10.1

情景: 从服务器同步数据。在本地添加条目(这与服务器不同步)。从服务器同步数据。

结果: 本地实体不在对象图中。 (我仍然可以在数据库中看到它,它只是没有连接)

JSON

{
  ThreadId: 322,
  ThreadMessages: 
  [    
    {
      Text: "Hello Joe",
      AuthorId: 635236,
      AuthorName: "Trudy",
      Timestamp: "2012-06-09T15:42:45.757",
      ThreadMessageId: 546,
      ThreadId: 322
    }
  ]
}

映射

threadMessageMapping = [RKManagedObjectMapping mappingForClass:[ThreadMessage class] inManagedObjectStore:objectStore];
threadMessageMapping.primaryKeyAttribute = @"threadMessageId";    
[threadMessageMapping mapKeyPath:@"ThreadMessageId" toAttribute:@"threadMessageId"];
[threadMessageMapping mapKeyPath:@"ThreadId" toAttribute:@"threadId"];
[threadMessageMapping mapKeyPath:@"Text" toAttribute:@"text"];
[threadMessageMapping mapKeyPath:@"AuthorId" toAttribute:@"authorId"];
[threadMessageMapping mapKeyPath:@"AuthorName" toAttribute:@"authorName"];
[threadMessageMapping mapKeyPath:@"Timestamp" toAttribute:@"timestamp"];


threadMapping = [RKManagedObjectMapping mappingForClass:[Thread class] inManagedObjectStore:objectStore];
threadMapping.primaryKeyAttribute = @"threadId";
threadMapping mapKeyPath:@"ThreadId" toAttribute:@"threadId"];
[threadMapping mapKeyPath:@"ThreadMessages" toRelationship:@"threadMessages" withMapping:ThreadMessageMapping];

详细信息: 我可以在我的应用程序中使用上面的映射。我还可以在本地插入一个工作正常的新ThreadMessage(它的threadMessageId为0)。当我从服务器同步并且Restkit / CoreData发现我的新项目时出现问题。我没有留在我的对象图中。调查sqlite db我发现这是因为ZTHREAD表中的ZTHREADMESSAGE链接正在被填充。

有没有办法让RestKit留下不是来自服务器的物品?

我也尝试使用connectRelationship来保湿连接,但似乎用我的json不可行

此行导致ZTHREAD中的ZTHREADMESSAGES列对所有实体显示为空白

[threadMapping connectRelationship:@"threadMessages" withObjectForPrimaryKeyAttribute:@"threadId"];

如果我映射了inverse relationsip,那么ZTHREAD列就可以了,但是当重新同步时,新的本地项仍会从对象图中删除。

[threadMessageMapping mapKeyPath:@"ThreadMessageId" toAttribute:@"threadMessageId"];
[threadMessageMapping mapRelationship:@"thread" withMapping:threadMapping];
[threadMessageMapping connectRelationship:@"thread" withObjectForPrimaryKeyAttribute:@"threadMessageId"];

我一直无法弄清楚RestKit在哪里使我的新ThreadMessage无效。我认为它位于RKObjectMappingOperation的某个地方,但我无法看到魔法在哪里发生。我只能看到第二次同步后ThreadMessage.ThreadId变为nill'ed。

0 个答案:

没有答案