2

Possible Duplicate:
Core Data vs SQLite 3

I have been using SQLite for saving data in my iOS application (for a table with over 20,000 rows max). However, I've encountered a problem with SQLite, where, when I try to use a SELECT statement while I insert data to the table, the SELECT statement won't work.

Would something like this work in Core Data? Can I retrieve data from the database at the same time as I insert new data into it?

Community
  • 1
  • 1
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • Speed difference: CoreData should be a bit slower as it's an OO (ObjC) wrapper around Sqlite3. Otherwise: SQLite doesn't permit you (AFAIK) to insert and get data at the same time. Why would you want this, anyway? –  Apr 23 '12 at 19:45
  • thx for the replay,i have a music app that i download data (songs details) from a web service(500 max each time). and when i have 2 pages(1000 songs) to get from the service i insert first 500 rows, and in the second time i insert again and in this time i can't make any SELECT on the table to get albums,artist, genres from this table. so i think to use CoreData and want to know what is the diffrents. – YosiFZ Apr 23 '12 at 19:50
  • 1
    I rewrote the question to focus on your core issue: can you read and write data to the database at the same time in Core Data. Asking for all of the differences between SQLite and Core Data is way too broad of a topic, and much of that ground has been covered by the question Jason links to. – Brad Larson Apr 23 '12 at 21:16

1 Answers1

3

Yes, you can do queries and inserts on Core Data at the same time, which I assume you'll want to do on multiple threads. The best way to do this is to have an NSManagedObjectContext for each thread (or queue).

The way I'd set it up is to have your main thread (UI) have an NSManagedObjectContext just for fetching the data, and one for inserting on a separate thread, with its parentContext set to the main one. That way when you save on the child context you will see those new objects in the main context and you can do a new fetch (or if you have an NSFetchedResultsController, it will update it for you).

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdConcurrency.html#//apple_ref/doc/uid/TP40003385

The WWDC 2011 videos about core data talk about the parentContext in detail.

Philippe Sabourin
  • 8,066
  • 3
  • 31
  • 46