2012-07-11 11 views
10

He revisado la respuesta en RestKit Object Mapping: difficulty using setObjectMapping:forResourcePathPattern:withFetchRequestBlock y está funcionando, pero solo para la última asignación. Ejemplo:RestKit eliminar los datos antiguos de los datos principales utilizando 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]; 
          }]; 

Todas las asignaciones funcionan a la perfección. Cuando se actualiza el xml fuente, se crean nuevos registros. Cuando elimino un Event, se elimina. Cuando elimino un AudioSource, no se borra.

Si elimino el segundo setObjectMapping:forResourcePathPattern:withFetchRequestBlock, entonces el AudioSource se borra correctamente, pero el Event borrado no lo está. Tengo 4 mapeos con los que estoy trabajando en este archivo xml.

Es como la última llamada a setObjectMapping:forResourcePathPattern:withFetchRequestBlock gana.

Mi solución consiste en utilizar el setObjectMapping:forResourcePathPattern:withFetchRequestBlock en la asignación que cambia más a menudo (en este caso, el Events) y agregar un botón que invalide el caché, vacíe la base de datos y actualice. Debe haber algo simple que me falta.

Xcode: archivo XML 0.10.1

muestra: 4.3.3 RestKit. Todo esto cargas muy bien, pero sólo elimina a partir de datos básicos de la asignación utilizando la última 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

¿Cómo es su XML fuente? ¿Tiene , y como etiquetas anidadas? –

+0

@Steven Hepting: he editado la pregunta para agregar una muestra del archivo xml. ¡Gracias por preguntar! – Kent

+0

¿Cómo estás borrando cosas? ¿Puedes agregar ese código? probablemente se sobrescriba una llamada. Verifique mi respuesta para que pueda depurar el comportamiento de los datos centrales. – clopez

Respuesta

2

No he utilizado objetos administrados antes, pero el primero que hay que hacer aquí es activar el registro de restkit sobre mapeo objeto, la red solicitud y centrales de datos para que pueda comprobar lo que es restkit llegar desde el servidor, cómo el mapeo está trabajando y cómo es ir a buscar las cosas desde un CD, a fin de tratar el siguiente:

//This can be added in your app delegate 
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); 
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); 
RKLogConfigureByName("RestKit/CoreData", RKLogLevelTrace); 

en cuanto a su código, se utiliza el la misma ruta para ambas asignaciones aquí:

// 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]; 
         }]; 

creo que esto puede ser la causa de un conflicto, porque RK elegir uno de los dos recursos que se asignan a ese camino, por lo que debe:

  1. depuración lo que está haciendo la base de datos.
  2. Trate de usar un mapeo para el enfoque de ruta clave en lugar del patrón de ruta de recursos, para que RK no se estropee, necesita definir diferentes formas de asignar cada tipo de objeto, en este momento creo que el primero se sobrescribe .

Si eso no funciona, debe publicar cómo está borrando cosas en su código, tal vez publicar todo el código de su controlador de vista. Lo que puede estar sucediendo es que las llamadas están siendo sobreescritas por su código en alguna parte. ¿Estás usando bloques ?.

Espero que ayude!

+0

Tengo la salida del mensaje de registro. No borro del código. Cuando edito el xml y elimino un elemento, en la próxima actualización los elementos deberían ser eliminados. Funciona para el último 'mappingProvider'. Los únicos bloques que estoy usando están en el anterior en 'withFetchRequestBlock'. Analizaré el mapeo del enfoque clave y volveré a los resultados. Gracias. – Kent

+0

¿Y cuál es la salida del registro de datos del núcleo cuando realiza tales operaciones? – clopez

Cuestiones relacionadas