Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Support Core Data Mapping without Primary Keys #95

Closed
blakewatters opened this Issue May 11, 2011 · 7 comments

Comments

Projects
None yet
3 participants
Owner

blakewatters commented May 11, 2011

Hi,

The RestKit documentation mentions:

"2. RestKit expects that each instance of an object be uniquely
identifiable via a single primary key that is present in the payload.
This allows the mapper to differentiate between new, updated and
removed objects."

Unfortunately this assumption is a very strong one, and the Rest API I
am working with (or rather, against), fails this assumption.

I am seeking for advice on how to work around this Rest Kit
requirement.

First, here is a payload that I would get back in the doc Project
sample if it was handled by my API:

{
         "id": 123,
         "name": "Produce RestKit Sample Code",
         "description": "We need more sample code!",
         "user": {
             "id": 1,
             "name": "Blake Watters",
             "email": "blake@twotoasters.com"
         },
         "tasks": [
         {"name": "Identify samples to write", "assigned_user_id":
1},
         {"name": "Write the code", "assigned_user_id": 1},
         {"name": "Push to Github", "assigned_user_id": 1},
         {"name": "Update the mailing list", "assigned_user_id": 1}
         ]
}

This example is extracted verbatim from the Rest Kit documentation
with only one change: the "tasks" in the project don't have an id any
more.

How would I handle such payloads with Rest Kit using Core Data? Note
that I would still prefer the "tasks" to be implemented as a one-to-
many relation from "Project" to "Task".

Of course, with such a Rest API, it becomes impossible to
differentiate between new, updated and removed objects tasks. Possible
behaviors in such a case could be either:

1- replace all existing tasks for the project in all cases with those
returned by the server
2- use a "by-value" semantics whereby the primary key is supposed to
be the composite of all the objects keys. (this would require that no
two tasks could possibly have exactly identical content).

In my case, I would want the first behavior, as two lines could have
the same values (think two identical expenses for example).

I got in touch with the Rest API vendor who said the reason the tasks
have no id in their API is because they are laid out in-line in their
data structure, not in a different table. They added that it would be
impossible to fulfill the RestKit requirement without completely
redesigning their system, which will never happen. Keep in mind that
the problem occurs everywhere in their API.

So I am looking for advice on how to handle that. As a newcomer to
Rest Kit, I would appreciate any idea. I could see two ways, neither
of which seem very appealing to me:

- Modify Rest Kit to relax this requirement
- Modify Rest Kit to generate Primary keys when they are missing (with
a UUID scheme, or an auto-increment scheme).

Any help really appreciated.
Owner

blakewatters commented Jun 10, 2011

See #103 also

Owner

blakewatters commented Jun 16, 2011

This should be handled already, we just need to put together a unit test proving it.

Owner

blakewatters commented Jun 29, 2011

This is supported.

kbekri commented Nov 29, 2012

I'm kind of new to RestKit, But finaly, how did you managed this particular case that I'm facing right now? (especialy with coreData creating multiple identical object).
In my case, deleting previous relationships will be greate (but only if I got a new response from serveur)

The same issue :(

Owner

blakewatters commented Dec 20, 2012

@kbekri @SPopenko There is an open issue on #1073 tracking a use case of mapping onto a relationship. Is that what you guys are doing? I am going to add support for that use-case soon. If you are not mapping onto a relationship and do not have any identifying attributes in the response, then I'm not sure what the library can do to help you. You'll have to delete the objects manually or clean them up with the orphaned object deletion support

@blakewatters thank you for your response. Yep I use it to map a relationship and have duplications. So, as a temporary solution I made a clean-up with the orphaned object deletion support :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment