George Asda
George Asda

Reputation: 2129

Design CoreData database

I'm sort of looking for advise how to structure an iOS core data app that uses pre-filled data. The problem: Users are presented with a pre-filled (photos) sqlite database (easy) but they can modify the default data (replace/delete photos). I want to keep the default photos on the main bundle without copying anything to the app directory but obviously I can not write to the main bundle anything if user decides to edit/replace the photos.

I do not wish to keep the photos inside the sqlite neither I want to copy the default photos to the app directory (as Apple doesn't like to add anything that the user didn't add to the app directory). The Question: How can I call the default photos from the main bundle but call the edited/replaced photos from the app directory?

any ideas would be much appreciated...

something like:

if (userEditedPhoto) {

imageView.image = //appDirectory:userPhoto

}

else {

imageView.image = //mainBundle:defaultPhoto

}

thanks

Upvotes: 0

Views: 91

Answers (1)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

For the Core Data side it is pretty simple:

  1. Create a Core Data based SQLite file that is included in your app bundle
  2. On launch create a new NSPersistentStore that lives in the document directory
  3. Add both stores to the NSPersistentStoreCoordinator.

Core Data will treat them as if they are a single store and you can read from both and write to one. Copying images that are being edited is fairly straight forward so I won't go into detail on that unless you have questions.

Update

Core Data is not a database, it is an object store. That object store can use as many separate files on disk as you want. my suggestion above was to have two files on disk, read from both and write to only one. Thus giving you your pre-fetched data.

Upvotes: 1

Related Questions