2012-04-04 5 views
0

Ich arbeite eine lange Zeit auf dieses Problem, konnte aber keine Lösung finden, die meinen Bedürfnissen entspricht.Spätes Laden von Remote-Daten (RestKit und CoreData)

Das Problem ist, wie kann ich Daten zu laden und ordnet sie der Beziehung ohne die gesamte Struktur

Einfaches Beispiel zu Laden:

Wir haben einige Vögel bekam:

{ 
    "birds":[{ 
      "bird":{"id":"1","value":"LaLeLu"}, 
      "bird":{"id":"2","value":"LeLeLa"}, 
      ... 
    }] 
} 

Dies könnte durch so etwas zugeordnet werden:

RKManagedObjectMapping *birdMapping = [RKManagedObjectMapping 
mappingForClass:[Bird class]]; 
menuMapping.primaryKeyAttribute = @"identifier"; 
[menuMapping mapKeyPath:@"value" toAttribute:@"value"]; 
[menuMapping mapKeyPath:@"id" toAttribute:@"identifier"]; 

[[[RKObjectManager sharedManager] mappingProvider] 
setMappingForKeyPath:"birds.bird"]; 

Funktioniert jetzt großartig.

Jetzt könnte jeder Vogel viele Kommentare haben - aber ich möchte nicht laden alle diese Kommentare mit der ersten Anfrage.

Kommentare sollten geladen werden, wenn Benutzer auf den bestimmten Vogel klicken. So

Ich bitte:

NSString *resourcePath = [NSString stringWithFormat:@"/birds/%@/ 
comments", myBird.id] 
[[RKObjectManager sharedManager] 
loadObjectsAtResourcePath:resourcePath]; 

Ich konnte die Antwort ändern, dass sie auf die Bedürfnisse der RestKit passt - aber was sind die Bedürfnisse?

{ 
    "comments":[{ 
      "comment"{"id":"1","value":"Comment1","bird_id":"1"} 
    }] 
} 

Und jetzt habe ich keine Idee, wie diese Antwort zugeordnet werden soll.

Abbildung der Kommentare ohne Bezug auf die Vögel kein Problem:

RKManagedObjectMapping *commentMapping = [RKManagedObjectMapping 
mappingForClass:[Comment class]]; 
menuMapping.primaryKeyAttribute = @"identifier"; 
[menuMapping mapKeyPath:@"value" toAttribute:@"value"]; 
[menuMapping mapKeyPath:@"id" toAttribute:@"identifier"]; 

[[[RKObjectManager sharedManager] mappingProvider] 
setMappingForKeyPath:"comments.comment"]; 

Hoffnung jemand mein Problem versteht und

Antwort

0

Für alle, die in der Lösung interessiert ware helfen könnte:

RestKit 0.10.0 behebt das Problem:

RKManagedObjectMapping *commentMapping = [RKManagedObjectMapping 
mappingForClass:[Comment class]]; 
commentMapping.primaryKeyAttribute = @"identifier"; 
[commentMapping mapKeyPath:@"value" toAttribute:@"value"]; 
[commentMapping mapKeyPath:@"id" toAttribute:@"identifier"]; 

// Here starts the relevant part: 

[commentMapping mapKeyPath:@"bird_id" to Attribute:@"bird_id"]; 
[commentMapping mapRelationship:@"bird" withMapping:birdMapping]; 
[commentMapping connectRelationship:@"bird" withObjectPropertyForPrimaryKeyAttribute:"bird_id"] 



[[[RKObjectManager sharedManager] mappingProvider] 
    setMapping:commentMapping ForKeyPath:"comments.comment"];