Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Proper procedure Relationship Mapping with primary key and initial sync
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Blake Watters  
View profile  
 More options Feb 15 2012, 11:28 am
From: Blake Watters <bl...@gateguruapp.com>
Date: Wed, 15 Feb 2012 11:28:09 -0500
Local: Wed, Feb 15 2012 11:28 am
Subject: Re: [RestKit] Re: Proper procedure Relationship Mapping with primary key and initial sync

Hi Bob et al -

There has been support for hydrating relationships on managed objects using primary key attributes for quite some time. This support requires you to do a couple of things:

1) Define an attribute mapping from your JSON payload to an attribute to keep track of the ID. (i.e. map user_id to userID on your mappable target entity)
2) Configure the RKManagedObjectMapping for your mappable target entity to include a relationship mapping for the relationship. So in the case of our user example, [objectMapping mapRelationship:@"user" withMapping:userMapping];
3) Configure your userMapping to indicate which attribute serves as the primary key for your object mapping. userMapping.primaryKeyAttribute = @"userID";
4) Configure the parent mapping to connect the relationship for you. [objectMapping connectRelationship:@"user" withObjectForPrimaryKeyAttribute:@"userID"];

RestKit will then connect the user relationship with the User entity where the primary key ID is equal to the mapped primary key attribute. No nested object are necessary.

Until this morning, this support worked exclusively for 1-to-1 relationships. I have just merged support that enables the support to work for one-to-many relationships such that you can send down an array of ID's and RestKit will fetch a set of objects and connect the one-to-many. It works identically as the above, with the exception that you need to configure your userID's (or whatever the attribute name is that will hold your object ID array) as a transformable so that RestKit can store the array on your mappable entity.

You can review the changes and supporting unit tests here: https://github.com/RestKit/RestKit/commit/789b0b99eb95c1c8f9f8bbf2663...

As to the larger concerns around syncing strategies, it is highly application dependent. I would love to get some generalized support in place at some point and there is a somewhat stale conversation around this pull request on Github: https://github.com/RestKit/RestKit/pull/197

I would love to continue that conversation on Github as the high email volume on this list makes it really hard for me to keep up and get anything meaningful done on the project simultaneously.

Cheers,
Blake

--
Blake Watters
VP Engineering, GateGuru
bl...@gateguruapp.com (mailto:bl...@gateguruapp.com)
Mobile: 919.260.3783
www. (http://www.gateguruapp.com)gateguruapp.com (http://gateguruapp.com)
Get GateGuru for iOS: bit.ly/ggitunes (http://bit.ly/ggitunes)
Get GateGuru for Android: bit.ly/ggandroid (http://bit.ly/ggandroid)

Interested in creating the mobile travel experience of the future? We're hiring! (http://gateguruapp.com/careers/)

On Tuesday, February 14, 2012 at 1:37 AM, Bob Spryn wrote:
> One further thing, my question about syncing is less along the lines
> of the post you pointed to (we actually have an almost identical
> setup) but more along the lines of the one to many relationship, or
> even more difficult a relationship going in both directions. Looks
> like the answer is pretty much the same. Restkit doesn't have that
> magic yet, and it would be custom code to handle it.

> On Feb 13, 5:46 pm, Dave Thompson <th03p...@gmail.com (http://gmail.com)> wrote:
> > Bob,

> > I picked up your mail. I'm fairly new to RestKit myself and probably
> > not the best person to answer all of your queries, but I'll take a
> > stab.

> > Let me paraphrase your thoughts into four questions:

> > 1) Is it possible to load two linked entities from two distinct JSON
> > payloads received at different times, such that a Core Data
> > relationship is hydrated between them at the time the second entity is
> > loaded?

> > Yes.

> > 2) How can this be done for 1-to-many relationships?

> > Where your Daughter entity is received after your Mother entity has
> > already been received and loaded, this is straightforward and
> > described in my response tohttp://groups.google.com/group/restkit/browse_thread/thread/c89d0bbb9....
> > As well as the steps I describe there, you'll need to make sure you
> > have the primary key set up on the Mother entity. In that example,
> > this would be done as: teamMapping.primaryKeyAttribute = @"teamId";.

> > 3) How can this be done for many-to-1 relationships?

> > Where your Mother entity is received after your Daughter entity has
> > already been received and loaded, things are trickier. There is no
> > built in RestKit support for this, as outlined in the questionhttp://groups.google.com/group/restkit/browse_thread/thread/cb5800bad...
> > . (An array of IDs doesn't work, nor do I think that would be a very
> > good implementation.) I therefore see two options that you have:

> > (a) Fork RestKit, add this functionality to it, and use your forked
> > copy of RestKit in your app. I don't know RestKit's internals, but my
> > gut feel is that this shouldn't be too difficult, given that the
> > implementation for (2) above already exists. This will likely result
> > in the cleanest implementation for your app. If you go down this
> > route, I'm sure they would be other people who would appreciate you
> > submitting the update back to Blake for inclusion in RestKit proper.

> > (b) Write some custom code within the objectLoader:didLoadObjects:
> > method in your RKObjectLoader delegate that handles the loading of the
> > Mother objects. For every Mother object loaded, retrieve the set of
> > Daughter objects with daughter.motherID == mother.motherID and
> > daughter.mother == nil. For every Daughter object in that set, set
> > daughter.mother = mother. (Recall from the linked question above that
> > Daughter objects must have both the relationship daughter.mother and
> > also the redundant foreign key daughter.motherID defined, to enable
> > RestKit's mapping.) This is the most straightforward solution to (3),
> > but your code will inevitable be less clean cut than if you did (a)
> > above.

> > 4) How does this fit into my syncing solution?

> > RestKit is not itself a syncing solution. As any specific syncing
> > solution is highly dependent on business rules, you'll most likely
> > need to build your own. There is a question on StackOverflow that I
> > think is very useful for this:http://stackoverflow.com/questions/5035132/how-to-sync-iphone-core-da...
> > . I personally find chris's answer to that question more useful than
> > the accepted answer, and will likely build my own syncing solution in
> > a similar manner to that he describes.

> > RestKit will, however, be hugely useful in managing the server
> > communication and object mapping within whatever syncing solution you
> > end up with.

> > I hope that helps,
> > Dave

> > On Feb 13, 5:16 am, Bob Spryn <bobsp...@gmail.com (http://gmail.com)> wrote:

> > > Bumping this as I'm sort of spinning my wheels until I figure this
> > > out.

> > > On Feb 8, 4:01 pm, Bob Spryn <bobsp...@gmail.com (http://gmail.com)> wrote:

> > > > So I'm trying to put together the proper procedure for doing
> > > > relationship mapping with keys instead of embedding the objects in the
> > > > json, as the embedding piece is really hammering our API and timeouts
> > > > are occurring.

> > > > I was looking at this other thread:http://groups.google.com/group/restkit/browse_thread/thread/b30075024...

> > > > Is there some documentation or an example somewhere of the exact
> > > > correct way to do this?

> > > > My followup question is this: Once my mapping with id's is working,
> > > > how should I perform an initial sync to pull down all the data? How
> > > > does this work when objects may be pointing via id relationship to
> > > > other objects that don't yet exist in core data? Is it going to make
> > > > an individual http request in the middle of a large request for a
> > > > singular object because of the relationship, or will it somehow batch
> > > > all these together?

> > > > Best,
> > > > Bob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.