2012-07-11 95 views
10

我已閱讀RestKit Object Mapping: difficulty using setObjectMapping:forResourcePathPattern:withFetchRequestBlock的答案,它正在工作,但僅適用於最後的映射。例如:RestKit使用setObjectMapping從核心數據中刪除舊數據

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更新時,將創建新記錄。當我刪除Event時,它會被刪除。當我刪除一個AudioSource它不會被刪除。

如果我刪除第二個setObjectMapping:forResourcePathPattern:withFetchRequestBlock那麼AudioSource被正確刪除,但刪除的Event不是。我在這個XML文件中使用了4個映射。

這就像是最後一次致電setObjectMapping:forResourcePathPattern:withFetchRequestBlock獲勝。

我的解決方法是在最常更改的映射(本例中爲Events)上使用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> 
+1

你的XML源代碼是什麼樣的?它是否有全都作爲嵌套標籤? – 2012-08-19 01:52:04

+0

@Steven Hepting - 我編輯了這個問題來添加一個xml文件的樣本。謝謝你的慰問! – Kent 2012-08-20 03:21:19

+0

你如何刪除東西?你可以添加代碼嗎?可能有一個電話被覆蓋。檢查我的響應,以便調試核心數據的行爲。 – clopez 2012-08-23 14:07:36

回答

2

我沒有使用過管理對象之前,但在這裏做的第一件事是激活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選擇這兩個資源的一個映射到該路徑,所以你應該:

  1. 調試什麼是核心數據做什麼。
  2. 嘗試使用映射的關鍵路徑方法,而不是資源路徑模式,所以RK不會混亂,你需要定義不同的方式來映射每種對象,現在我認爲第一個被覆蓋。

如果這不起作用,你應該發佈如何刪除代碼中的東西,也許發佈你的視圖控制器的所有代碼。可能發生的情況是,調用被您的代碼覆蓋。你在使用塊嗎?

希望有幫助!

+0

我有日誌消息輸出。我不會從代碼中刪除。當我編輯xml並刪除元素時,在下次更新時,元素應該被刪除。它爲最後一個'mappingProvider'工作。我使用的唯一塊在上面的'withFetchRequestBlock'中。我將研究關鍵方法的映射並回顧結果。謝謝。 – Kent 2012-08-24 14:17:32

+0

當您執行此類操作時,核心數據日誌的輸出是什麼? – clopez 2012-08-24 14:57:54