Restkit with (complex) relationships and Core Data / RAILS
Please bear with me as I'm just learning Cocoa. I've been through some tutorials concerning Core Data, I'm now tackling RESTkit to use it with a Rails application. I also read all docs, solutions recipes on the RESTkit wiki page (espcially iOS SDK: Advanced RestKit Development was very useful).
For the sake of simplicity I'm going to use a very limited number of models/relationships in this question.
Suppose I have the following models: company, person, language with following relations:
company many-to-many person
language 1-to-many person
and that both the RAILS and the iOS app are in sync.
At a certain time a new person is introduced into the Rails app which is related to an existing company and existing language: eg. Steve Jobs > Disney, Steve Jobs > English At the same time a new person is added to a new company, and a new language eg. Anna Kourinikova > Nike, Anna Kournikova > Russian
Now how do I set up a syncing solution ? I could ask for a JSON dump of all new people:
[{"person": {
"id": 123,
"name": "Steve Jobs",
"language": {
"id": 1,
"name": "English"
},
"company": {
"id": 1,
"name": "Disney"
}
}},
{"person": {
"id": 124,
"name": "Anna Kournikova",
"language": {
"id": 22,
"name": "Russian"
},
"company": {
"id": 47,
"name": "Nike"
}
}}]
Now my questions:
1) can (and if yes how?) RESTkit create and link the new company and l开发者_Go百科anguage. I suppose for the existing company it's not a problem, but the new ones don't exist yet in my Core Data.
2) is there a way to avoid the need to include all the company and language data, as in the real world application this would create an enormous overhead of data which already exists on the iOS device if people are added to existing companies
3) may be the approach of first fetching all new languages, all new companies and then fetching al new people, with only the id of the language/company to which they're related to is a better approach in that way (to conserve bandwith) but am I not doing to many things manually that RESTkit can automatically do ?
4) what if an existing person is linked to a second company?
As this seems like a very real world situation I think it strange that there aren't any (similar) examples in the documentation (I'm willing to write a tutorial on this subject provided if I can work things out with your help.)
Let me try and address your questions one at a time:
- Yes, with the proper mapping setup, RestKit can create new Companies and Languages from the above JSON.
Yes, you can avoid fetching the nested company and language data in favor of company_id and language_id foreign keys which can be used at mapping time to connect a new Person to existing Companies and/or Languages in your local store. Check out the following mapping API:
- (void)connectRelationship:(NSString*)relationshipName withObjectForPrimaryKeyAttribute:(NSString*)primaryKeyAttribute;
For #3 & #4, you're going to need to solidify your server API design before these can be concretely answered. You've suggested that you'll use the nested JSON in the question, but have also asked questions suggesting you may not use the nested JSON and may even break up the above JSON into multiple requests. The scenario you describe in #4 can be handled by RestKit, but the specifics of how really depend on what calls your making to the server and what payload is coming back in response to those calls. Happy to provide more help here, but at the end of the day, I'm extremely confident RestKit can handle your use cases, with the specifics of your mapping configuration greatly dependent on you finalizing the server API.
精彩评论