I plan to use CoreData API for storing files in my iOS app. I want to have two stores of data: first, large, but possibly temporary, for caching; and second - small, but I want to ensure the data is persistent and never deleted. What are the best practices for doing this?
- 4,093
- 9
- 45
- 78
-
You want to store files, as in files of data? Or you want to store objects, in Core Data, in a database file? – Wain May 17 '13 at 07:20
-
@Wain I prefer using CoreData, but if there's an alternative solution which ensures persistency I'd use it – Yury Pogrebnyak May 17 '13 at 07:23
-
But what are you trying to store, what is the data? – Wain May 17 '13 at 07:24
-
@Wain only objects: some arrays of primitives and more complex objects – Yury Pogrebnyak May 17 '13 at 07:33
1 Answers
You need to create 2 separate Core Data 'stacks' - i.e. 2 different models (assuming the stored data is different in each), persistent stores, persistent store coordinators and managed object contexts. Both stacks will save the model to a file, but your temporary file should save into NSTemporaryDirectory (or perhaps better a cache directory) whereas your permanent file should be saved into NSHomeDirectory.
Other than that the usage of Core Data is nothing special. You just need to use the appropriate managed object context for the data you are saving / retrieving.
If you wanted to move any objects from one store to the other you would need to write the code to do that (i.e. get the object, create a new object in the other store and then copy each attribute across - use dictionaryWithValuesForKeys: and setValuesForKeysWithDictionary:).
- 118,658
- 15
- 128
- 151
-
This is correct, but using only one model and one moc can be more confortable ;-) [link](http://stackoverflow.com/questions/5231775/can-multiple-two-persistent-stores-be-used-with-one-object-model-while-mainta) – LombaX May 17 '13 at 08:09