Domanda

Dopo aver letto il Object Mapping-guida su GitHub per RestKit il mio problema didn 't scompaiono, quindi forse qualcuno può dire se RestKit potrebbe trattare con la seguente idea.

> Entità con relazioni

 Company 
 - unitID 
 - companyID
 - name
 - contacts* (Company -->> Contact | 1:n)

 Contact
 - unitID
 - companyID
 - contactID
 - lastName
 - firstName
 - account* (Contact >--> Company | 1:1)

JSON (Società)

 [
   {
      "unitID":"003CABD8DEB5DC13C",
      "companyID":"BSP-002999",
      "name":"Testcompany"
   }
 ]

JSON (contatto)

 [
   {
      "unitID":"DAC2ACCC125795D00",
      "companyID":"BSP-002999",
      "contactID":"CLP-015468",
      "firstName":"Mister",
      "lastName":"Wayne"
   }
 ]

A causa delle limitazioni non sono in grado di nidificare i contatti appartenenti nelle aziende (altrimenti non sarei scrivere questo), quindi voglio fare questo uso, quando i dati sono mappati durante l'importazione.

Domanda

E 'possibile mappare ogni contatto è appartenenza Company (identificato dall'attributo CompanyID ) all'importazione con determinati metodi da RestKit?

Se no, mi piacerebbe conoscere la soluzione migliore per questo. Grazie!


Sfondo

Nel mio primo build di questa applicazione ho mappato gli oggetti alle entità di dati (senza relazioni) e prese tutti i contatti appartenenti ad una società con il predicato CompanyID =% @ . A causa della quantità di dati (4000+ aziende, 7000+ Contatti) che vanno a prendere tutti i contatti appartenenti richiede circa un secondo -. Così mi è venuta l'idea di utilizzare relationsships (che funziona perfettamente con i dati manichino locale)


Soluzione

Con l'aiuto della risposta data al di sotto del mio aspetto mappatura attuali come la seguente (RestKit v.10).

// Setting up Restkit with objectStore
...

// Init objectMapping for Class Company
companyMapping = [RKManagedObjectMapping mappingForClass:[Company class] inManagedObjectStore:objectStore];
[companyMapping mapKeyPath:@"unitID" toAttribute:@"unitID"];
[companyMapping mapKeyPath:@"companyID" toAttribute:@"companyID"];
[companyMapping mapKeyPath:@"name" toAttribute:@"name"];
companyMapping.setDefaultValueForMissingAttributes = NO;
companyMapping.primaryKeyAttribute = @"companyID";

// Init objectMapping for Class Contact
contactMapping = [RKManagedObjectMapping mappingForClass:[Contact class] inManagedObjectStore:objectStore];
[contactMapping mapKeyPath:@"unitID" toAttribute:@"unitID"];
[contactMapping mapKeyPath:@"companyID" toAttribute:@"companyID"];
[contactMapping mapKeyPath:@"contactID" toAttribute:@"contactID"];
[contactMapping mapKeyPath:@"lastName" toAttribute:@"lastName"];
[contactMapping mapKeyPath:@"firstName" toAttribute:@"firstName"];
contactMapping.setDefaultValueForMissingAttributes = NO;
contactMapping.primaryKeyAttribute = @"contactID";

// Init relationships
[contactMapping mapRelationship:@"company" withMapping:companyMapping];
[contactMapping connectRelationship:@"company" withObjectForPrimaryKeyAttribute:@"companyID"];

// Get objects from server
...

È stato utile?

Soluzione

Yes, RestKit can hydrate this relationship for you at mapping time. On your RKManagedObjectMapping for your Contact model, you will need to do the following:

  • Add a relationship mapping to the parent Company: [objectMapping mapRelationship:@"company" withMapping:companyMapping]
  • Map the companyID attribute onto your Contact model
  • Ensure that you have the primaryKeyAttribute configured on both classes
  • Instruct RestKit to hydrate the relationship: [objectMapping connectRelationship:@"company" withObjectForPrimaryKeyAttribute:@"companyID"];

After RestKit has completed mapping a payload, it will pass back through the collection and hydrate any relationships. The processing is deferred until after mapping in case the payload actually creates objects that you need to satisfy the connections.

On the development branch, there is an alternative workflow available to you. Earlier this week I added support for hydrating a has-many relationship using an array of ID's in the payload. This would let you send the list of Contacts that belong to a company down in the payload instead. It may be more efficient for your use case.

Additional details from my post to the RestKit mailing list: http://groups.google.com/group/restkit/msg/416951f86b2862d1

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top