views:

595

answers:

2

Hello,

I'am currently migrating an iphone application using SQLite to CoreData.

I need to do an INSERT or REPLACE to add only the new content, is there a way to do this or do I have to fetch all the DB and look for existing objects and add the new ones ?

Thanks.

+6  A: 

Remember, Core Data is an object hierarchy that happens to persist to a DB so you need to look at it like an object graph and not a database.

Therefore, yes you need to check to see if the object exists already using some defined unique ID and if it does not currently exist you need to create the new object vs. updating the existing object.

update

You don't need to fetch all of the objects, search the store using a NSFetchRequest and a NSPredicate to check for existence; if it exists update, if it doesn't create it.

Marcus S. Zarra
+4  A: 

See this Apple document, titled "Efficiently Importing Data." This is exactly the topic you'd like to learn. In particular, read the section "Implementing Find-or-Create Efficiently" carefully.

And of course, buy Marcus Zarra's book on Core Data which is fantastic :)

Yuji