objective-c – RestKit使用setObjectMapping从核心数据中删除旧数据

前端之家收集整理的这篇文章主要介绍了objective-c – RestKit使用setObjectMapping从核心数据中删除旧数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经在 RestKit Object Mapping: difficulty using setObjectMapping:forResourcePathPattern:withFetchRequestBlock审查了答案,它正在工作,但只适用于最后的映射.例:
  1. RKManagedObjectMapping *audioSourcesMapping = [RKManagedObjectMapping mappingForEntityWithName:kEntityAudioSources inManagedObjectStore:objectStore];
  2. [audioSourcesMapping mapKeyPath:@"icon" toAttribute:@"icon"];
  3. [audioSourcesMapping mapKeyPath:@"name" toAttribute:@"name"];
  4. [audioSourcesMapping mapKeyPath:@"notes" toAttribute:@"notes"];
  5. [audioSourcesMapping mapKeyPath:@"section" toAttribute:@"section"];
  6. [audioSourcesMapping mapKeyPath:@"url" toAttribute:@"url"];
  7. audioSourcesMapping.primaryKeyAttribute = @"name";
  8. [wsiObjectManager.mappingProvider registerMapping:audioSourcesMapping withRootKeyPath:@"winSystem.winSystemAudioSources.winSystemAudioSource"];
  9.  
  10.  
  11. [wsiObjectManager.mappingProvider setObjectMapping:audioSourcesMapping forResourcePathPattern:kWinSystemInfoXml
  12. withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
  13. return [AudioSources fetchRequest];
  14. }];
  15.  
  16.  
  17. RKManagedObjectMapping *eventsMapping = [RKManagedObjectMapping mappingForEntityWithName:kEntityEvents inManagedObjectStore:objectStore];
  18. [eventsMapping mapKeyPath:@"contact" toAttribute:@"contact"];
  19. [eventsMapping mapKeyPath:@"startDate" toAttribute:@"startDate"];
  20. [eventsMapping mapKeyPath:@"endDate" toAttribute:@"endDate"];
  21. [eventsMapping mapKeyPath:@"icon" toAttribute:@"icon"];
  22. [eventsMapping mapKeyPath:@"location" toAttribute:@"location"];
  23. [eventsMapping mapKeyPath:@"name" toAttribute:@"name"];
  24. [eventsMapping mapKeyPath:@"notes" toAttribute:@"notes"];
  25. [eventsMapping mapKeyPath:@"section" toAttribute:@"section"];
  26. [eventsMapping mapKeyPath:@"url" toAttribute:@"url"];
  27. eventsMapping.primaryKeyAttribute = @"name";
  28. [wsiObjectManager.mappingProvider registerMapping:eventsMapping withRootKeyPath:@"winSystem.winSystemEvents.winSystemEvent"];
  29.  
  30.  
  31. [wsiObjectManager.mappingProvider setObjectMapping:eventsMapping forResourcePathPattern:kWinSystemInfoXml
  32. withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
  33. return [Events fetchRequest];
  34. }];

所有的映射都很好.当源xml被更新时,将创建新的记录.当我删除一个事件时,它被删除.当我删除一个AudioSource它不会被删除.

如果我删除第二个setObjectMapping:forResourcePathPattern:withFetchRequestBlock,那么AudioSource被正确删除,但删除的事件不是.我在这个xml文件中有4个映射.

就像最后一次调用setObjectMapping:forResourcePathPattern:withFetchRequestBlock获胜.

我的解决方法是使用setObjectMapping:forResourcePathPattern:withFetchRequestBlock对最常更改的映射(在本例中为事件),并添加一个无效缓存,清空数据库和更新的按钮.必须有一些简单的东西我失踪了.

Xcode:4.3.3
RestKit:0.10.1

示例xml文件.这一切都很好,但只能从核心数据中删除使用最后一个setObjectMapping的映射:forResourcePathPattern:withFetchRequestBlock

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <winSystem>
  3. <winSystemAudioSources>
  4. <winSystemAudioSource
  5. icon="audio.png"
  6. name="Hub Audio"
  7. 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."
  8. section=" WIN System"
  9. url="http://stream.winsystem.org:443/2560.mp3" />
  10. </winSystemAudioSources>
  11. <winSystemEvents>
  12. <winSystemEvent
  13. contact=""
  14. endDate=""
  15. icon="net.png"
  16. location="WIN System reflector 9100"
  17. name="Insomniac Trivia Net"
  18. notes="Every Night @ 23:00 PT - WIN System reflector 9100. Join the Yahoo! group: http://groups.yahoo.com/group/insomniac-net/join"
  19. section="Ham Nets"
  20. startDate=""
  21. url="http://www.thedeanfamily.com/WinSystem/InsomniacNet.htm" />
  22. </winSystemEvents>
  23. <winSystemLinks>
  24. <winSystemLink
  25. icon="winsystem.png"
  26. name=" WIN System Home Page"
  27. notes="The WIN System Home Page"
  28. section=" WIN System"
  29. type="web"
  30. url="http://www.winsystem.org/" />
  31. </winSystemLinks>
  32. <winSystemRepeaters>
  33. <winSystemRepeater
  34. callSign="K6JSI"
  35. freqOffsetPl="448.800* (-) 100.0"
  36. grouping="winsystem"
  37. latitudeDefault=""
  38. locationElevation="Shorty's house,560' + 53'"
  39. longitudeDefault=""
  40. node="A 01330"
  41. repeaterId="1"
  42. serviceArea="Vista"
  43. serviceState="CA" />
  44. </winSystemRepeaters>
  45. </winSystem>

解决方法

我以前没有使用过管理对象,但是首先要做的是激活对象映射,网络请求和核心数据的restkit日志,以便您可以检查从服务器获取的是什么是restkit,映射如何工作,以及如何从CD获取东西,所以请尝试以下操作:
  1. //This can be added in your app delegate
  2. RKLogConfigureByName("RestKit/Network",RKLogLevelTrace);
  3. RKLogConfigureByName("RestKit/ObjectMapping",RKLogLevelTrace);
  4. RKLogConfigureByName("RestKit/CoreData",RKLogLevelTrace);

看看你的代码,你在这里使用相同的路径两个映射:

  1. // forResourcePathPattern:kWinSystemInfoXml
  2. [wsiObjectManager.mappingProvider setObjectMapping:audioSourcesMapping forResourcePathPattern:kWinSystemInfoXml
  3. withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
  4. return [AudioSources fetchRequest];
  5. }];
  6.  
  7. // forResourcePathPattern:kWinSystemInfoXml
  8. [wsiObjectManager.mappingProvider setObjectMapping:eventsMapping forResourcePathPattern:kWinSystemInfoXml
  9. withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
  10. return [Events fetchRequest];
  11. }];

我认为这可能导致冲突,因为RK选择两个资源之一映射到该路径,因此您应该:

>调试核心数据在做什么?
>尝试使用关键路径方法的映射而不是资源路径模式,所以RK不会搞砸,你需要定义不同的方法来映射每种对象,现在我认为第一个被覆盖.

如果这不起作用,你应该发布你如何删除代码中的东西,也许发布你的视图控制器的所有代码.可能发生的是,呼叫被您的代码覆盖在某处.你使用块吗?

希望有帮助!

猜你在找的C&C++相关文章