0

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

George Asda
  • 2,119
  • 2
  • 28
  • 54

1 Answers1

1

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.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • @Marcus I would think that creating two image models (one transient, one persisted) where the transient one would be dynamically loaded from the app bundle at runtime would be the way to go. That way the images could go into the app resource bundle and he could use a single Core Data graph for both (while still keeping a logical separation). Unless I am missing something. – smyrgl May 05 '14 at 18:09
  • But if I'm reading from one and writing to the other how is it gonna distinguish between the edited/replaced images if user decides to replace a pre-loaded image? It would always read from the pre-loaded.... – George Asda May 28 '14 at 12:46
  • @GeorgeAsda That is a coding problem that I am certain you could solve. You could put a state attribute in the entity to distinguish. You could keep track of the `NSPersistentStore`. I am sure there are a lot of other ways thats would occur to you. – Marcus S. Zarra May 28 '14 at 14:35
  • @smyrgl if it is loaded from the app bundle it is not transient. By flagging the app bundle store as read only, writing would always go to the other store which is what the OP wants. – Marcus S. Zarra May 28 '14 at 14:35
  • I think it is too complicated to even try this. What I'm gonna do is: install the database to documents directory (read from main bundle). If/when user decides to edit the database copy everything to documents directory and from there onwards read and write to documents directory. Apple has seriously f#!^# us up with not allowing to write large files to documents directory. – George Asda May 30 '14 at 06:58
  • The solution is not complicated, it is just a different approach than you are accustomed to. As is Core Data itself. Good luck with your solution. – Marcus S. Zarra May 30 '14 at 13:01