views:

40

answers:

2

Hi,

I would like to know if there is some kind of similar functionality or way to preform an "on duplicate key update" function with core-data as there is with MySQL.

What I want to do is to save an object to the database every time a user presses a button. But if the button is already pressed I want to update the row with some new values instead of adding a new row.

The only way I currently know how to do this is to read the rows from the DB, see if the row exists and then update it.. otherwise add a new row. This seems kind of bad to do this way, am I wrong?

A: 

EDIT

Remember that the core data framework manages persistence of your object graph. It is not an interface to a sqlite database.

Worry about your object life cycle. When do instances get created? When are they destroyed? What makes your instances unique? Using Books as an example entity, a book has an ISBN which is a unique way of identifying a title, yet many copies of each title can exist. You have two choices in your Entity model, you can create separate instances for each copy of the title or have one instance with a count attribute.

The sample projects CoreDataBooks and iPhoneCoreDataRecipes use NSUndoManager to track state changes between views.

falconcreek
Stop thinking about rows in a table. Objects can only exist if they were inserted to the context or fetched from the store.
falconcreek
Because I am quite new to this, could you show me an example of what you mean.. maybe a link you recommend or so?
Paul Peelen
i will update my answer. stay tuned.
falconcreek
updated answer with design thoughts
falconcreek
I do not agree with this design at all. It is very inefficient and fragile. The example projects pointed to do not show the best solutions for this simple problem. See my answer.
Marcus S. Zarra
The problem wasn't that clear at all. The question does not tell us if new objects are being created or just updating the state of an existing managed object. I will look at the updated projects. Thank you for views on the separate context.
falconcreek
+1  A: 

The easiest answer to this is to run a query against the Core Data context and get the object back if it exists. This is the most efficient and least error prone solution to the problem.

You do not need to create a separate NSManagedObjectContext and attempt to deal with merge policies, that is a very inefficient and dangerous way to try and resolve such a simple issue.

Core Data handles a lot of caching for you in the background. If you are attempting to retrieve an object that you just created there is a very high probability that it is still sitting in the cache so the response to your query will be nearly instantaneous.

Note

I just went back to both of those sample projects again to file a bug against them and noticed that they have been updated and finally removed the suggestion of creating a new context. They are now using the NSUndoManager where appropriate and a single context.

Marcus S. Zarra
Thank you for your answer. I don't really have the time to look at the samples because my deadline got moved. So what I will do is to implement your suggestion and then afterwards check out the sample project and see what I can do with the NSUndoManager. Thank you all for your answer.Regards,Paul Peelen
Paul Peelen