CQM
CQM

Reputation: 44268

RestKit serialization creating duplicate entries

When saving to my database with Restkit, I get duplicate entries.

I'm not sure how to prevent this. The intended behavior is that if the object already exists, then it should update that existing object with the columns that happen to be different.

I set a key identifier here:

[mapping setIdentificationAttributes:@[MYObjectAttributes.userID]];

but I suppose there is something else I am supposed to do. I've seen other questions more related to core-data that manually do a fetch request looking for an existing entry, before writing it, this seems expensive and restkit is supposed to have a solution for this already.

Upvotes: 2

Views: 112

Answers (2)

Ankit Thakur
Ankit Thakur

Reputation: 4749

Along with attributes, you can also detect, whether managed object is new or not. RestKit has created a category over NSManagedObject, where it has provided 1 function:

/**
 * Returns YES when an object has not been saved to the managed object context yet
 */
@property (nonatomic, readonly) BOOL isNew;

https://github.com/RestKit/RestKit/blob/fc101de9133d96bc0e2221153de7f699f8c1f06d/Code/CoreData/NSManagedObject%2BRKAdditions.m

Upvotes: 1

Erik Johansson
Erik Johansson

Reputation: 1248

RestKit is for mapping a RESTful service to core data. If you are not using the RKObjectManager for updating (that is, you want to put something on your REST service) and only want to do a local change you should get the managed object and work with it outside the context of RestKit.

If you need to check whether a managed object exists locally or not, you should do it with a Managed Object Context rather than try to use RestKit for it.

Upvotes: 1

Related Questions