Objective c RestKit使用setObjectMapping从核心数据中删除旧数据
我已经在查看了答案,它正在运行,但仅适用于最后一个映射。例如:Objective c RestKit使用setObjectMapping从核心数据中删除旧数据,objective-c,ios,xml,restkit,Objective C,Ios,Xml,Restkit,我已经在查看了答案,它正在运行,但仅适用于最后一个映射。例如: RKManagedObjectMapping *audioSourcesMapping = [RKManagedObjectMapping mappingForEntityWithName:kEntityAudioSources inManagedObjectStore:objectStore]; [audioSourcesMapping mapKeyPath:@"icon" toAttribute:@"icon"]; [audio
RKManagedObjectMapping *audioSourcesMapping = [RKManagedObjectMapping mappingForEntityWithName:kEntityAudioSources inManagedObjectStore:objectStore];
[audioSourcesMapping mapKeyPath:@"icon" toAttribute:@"icon"];
[audioSourcesMapping mapKeyPath:@"name" toAttribute:@"name"];
[audioSourcesMapping mapKeyPath:@"notes" toAttribute:@"notes"];
[audioSourcesMapping mapKeyPath:@"section" toAttribute:@"section"];
[audioSourcesMapping mapKeyPath:@"url" toAttribute:@"url"];
audioSourcesMapping.primaryKeyAttribute = @"name";
[wsiObjectManager.mappingProvider registerMapping:audioSourcesMapping withRootKeyPath:@"winSystem.winSystemAudioSources.winSystemAudioSource"];
[wsiObjectManager.mappingProvider setObjectMapping:audioSourcesMapping forResourcePathPattern:kWinSystemInfoXml
withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
return [AudioSources fetchRequest];
}];
RKManagedObjectMapping *eventsMapping = [RKManagedObjectMapping mappingForEntityWithName:kEntityEvents inManagedObjectStore:objectStore];
[eventsMapping mapKeyPath:@"contact" toAttribute:@"contact"];
[eventsMapping mapKeyPath:@"startDate" toAttribute:@"startDate"];
[eventsMapping mapKeyPath:@"endDate" toAttribute:@"endDate"];
[eventsMapping mapKeyPath:@"icon" toAttribute:@"icon"];
[eventsMapping mapKeyPath:@"location" toAttribute:@"location"];
[eventsMapping mapKeyPath:@"name" toAttribute:@"name"];
[eventsMapping mapKeyPath:@"notes" toAttribute:@"notes"];
[eventsMapping mapKeyPath:@"section" toAttribute:@"section"];
[eventsMapping mapKeyPath:@"url" toAttribute:@"url"];
eventsMapping.primaryKeyAttribute = @"name";
[wsiObjectManager.mappingProvider registerMapping:eventsMapping withRootKeyPath:@"winSystem.winSystemEvents.winSystemEvent"];
[wsiObjectManager.mappingProvider setObjectMapping:eventsMapping forResourcePathPattern:kWinSystemInfoXml
withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
return [Events fetchRequest];
}];
所有映射都工作得很好。更新源xml时,将创建新记录。当我删除事件时,它会被删除。当我删除音频源时,它不会被删除
如果删除第二个setObjectMapping:forResourcePathPattern:withFetchRequestBlock
,则AudioSource
被正确删除,但已删除的事件
未被正确删除。我在这个xml文件中使用了4个映射
这就像上次调用setObjectMapping:forResourcePathPattern:withFetchRequestBlock
wins一样
我的解决方法是对最常更改的映射(在本例中为事件
)使用setObjectMapping:forResourcePathPattern:withFetchRequestBlock
),并添加一个使缓存无效、清空数据库和更新的按钮。我一定错过了一些简单的东西
Xcode:4.3.3
RestKit:0.10.1
示例xml文件。这一切都可以正常加载,但只会使用最后一个setObjectMapping:forResourcePathPattern:withFetchRequestBlock从核心数据中删除映射
<?xml version="1.0" encoding="UTF-8"?>
<winSystem>
<winSystemAudioSources>
<winSystemAudioSource
icon="audio.png"
name="Hub Audio"
notes="Cleaner Sound. Audio is delayed by about 30 seconds. This is a great way to see if you are making into the WIN System."
section=" WIN System"
url="http://stream.winsystem.org:443/2560.mp3" />
</winSystemAudioSources>
<winSystemEvents>
<winSystemEvent
contact=""
endDate=""
icon="net.png"
location="WIN System reflector 9100"
name="Insomniac Trivia Net"
notes="Every Night @ 23:00 PT - WIN System reflector 9100. Join the Yahoo! group: http://groups.yahoo.com/group/insomniac-net/join"
section="Ham Nets"
startDate=""
url="http://www.thedeanfamily.com/WinSystem/InsomniacNet.htm" />
</winSystemEvents>
<winSystemLinks>
<winSystemLink
icon="winsystem.png"
name=" WIN System Home Page"
notes="The WIN System Home Page"
section=" WIN System"
type="web"
url="http://www.winsystem.org/" />
</winSystemLinks>
<winSystemRepeaters>
<winSystemRepeater
callSign="K6JSI"
freqOffsetPl="448.800* (-) 100.0"
grouping="winsystem"
latitudeDefault=""
locationElevation="Shorty's house, 560' + 53'"
longitudeDefault=""
node="A 01330"
repeaterId="1"
serviceArea="Vista"
serviceState="CA" />
</winSystemRepeaters>
</winSystem>
我以前没有使用过托管对象,但这里要做的第一件事是通过对象映射、网络请求和核心数据激活restkit日志,以便您可以检查restkit从服务器获取的内容、映射如何工作以及如何从CD获取内容,因此请尝试以下操作:
//This can be added in your app delegate
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/CoreData", RKLogLevelTrace);
查看您的代码,您在这里为两个映射使用相同的路径:
// forResourcePathPattern:kWinSystemInfoXml
[wsiObjectManager.mappingProvider setObjectMapping:audioSourcesMapping forResourcePathPattern:kWinSystemInfoXml
withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
return [AudioSources fetchRequest];
}];
// forResourcePathPattern:kWinSystemInfoXml
[wsiObjectManager.mappingProvider setObjectMapping:eventsMapping forResourcePathPattern:kWinSystemInfoXml
withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
return [Events fetchRequest];
}];
我认为这可能会导致冲突,因为RK选择两种资源中的一种映射到该路径,因此您应该:
调试核心数据正在做什么
尝试使用关键路径的映射方法而不是资源路径模式,这样RK就不会混乱,您需要定义不同的方法来映射每种对象,现在我认为第一种方法正在被覆盖
如果这不起作用,您应该发布如何删除代码中的内容,也许可以发布视图控制器中的所有代码。可能发生的情况是调用正在被代码覆盖。你在用积木吗
希望有帮助 您的源XML看起来像什么?它是否有,并且都是嵌套标记?@Steven Hepting-我已经编辑了这个问题,添加了一个xml文件示例。谢谢你的邀请!你是怎么删除东西的?你能加上那个代码吗?可能有一个呼叫被覆盖了。请检查我的响应,以便调试核心数据的行为。@clopez-如果我删除其中一个xml元素-例如,一个的。并添加一些新的。添加了新的,但删除的仍在核心数据中。我有日志消息输出。我不从代码中删除。当我编辑xml并删除元素时,在下一次更新时,应该删除元素。它适用于最后一个映射提供程序
。我使用的唯一块位于上面的withFetchRequestBlock
中。我将研究关键方法的映射并返回结果。谢谢。当您执行这些操作时,核心数据日志的输出是什么?