Two persistent stores for one managed object context - possible? - objective-c

I have a fairly complex data model with approximately 10 entities. Some need to be stored to disk and others just need to be available in memory when the application is running. Is it possible to achieve this using two persistent stores for the same managed object context, or should I separate my data models accordingly?

Yes, your NSManagedObjectContext uses a NSPersistentStoreCoordinator to determine which store a particular model should use. By setting the persistent store coordinator of your managed object context you can define a custom mapping which uses multiple persistent stores of different types.
http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650-SW4

You may use configurations as TechZen mentioned:
Create Configurations in managed object model editor (.xcdatamodel file);
In code add several persistent stores to persistent store coordinator, providing appropriate configuration name.
For details check my other answer here.

Related

Rebus Saga with Complex Data

I am developing a Application in .net core 6 using Rebus Sagas. I need my Saga Data to contain an array objects that will be removed from the list once an item is processed so that the system can know when all the items in the batch has been processed. What structure can I use to hold that information or what is the best way to organization that information.
I have come to an understanding that SagaData is persisted as a whole so any object in that structure will be serialized and saved to the database any time it is modified so you can put any structure in the ISageData that can be serialized and persisted.
This Solves the Problem.

Application of data objects in BPMN

Recently I've started a ‌BPMS project. I have read a lot about BPMN2 but I do not know exactly data objects and data store application.
In some cases data object is used to display files. For example in this diagram, Upload Expense Support File looks for a file but Data Input construct for modeling input data within the Process flow!!??
In other case data object is used to instance of Folio class (Folio table in database)
I'm confused. What is application of data object?
Thanks.
Data Stores are permanent. The information a process writes to a Data Stores is available after the process instance terminates.
Data Objects exists for the scope of the process. Only if a Data Object is defined as a Data Output Object it exists beyond the scope of the process.
So you should use Data Objects for explicit process inputs/outputs and documents that are created ad-hoc and later discarded.
Data Stores, should be used if your process creates, alters or consumes to generally persisting information.

Using two core data models, one persistent store coordinator, and multiple persistent stores?

I was wondering how the code for this image would look
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Art/advanced_persistence_stack.gif
I've tried to do it but I ended up saving both core data models to one sqlite database. How could you do as it is in the picture?
Thanks in advance!
You direct specific entities to a persistent store using the configuration part of your data model (that bit at the bottom that everyone ignores). Add two new configurations, one for each persistent store, then add the relevant entities to each one.
Then, when adding persistent stores during the setup of your core data stack, use addPersistentStoreWithType:configuration:URL:options:error:, with the name of your configuration in the configuration parameter.
The coordinator will then save entities to the appropriate store for you.

NSManagedObjectContext confusion

I am learning about CoreData. Obviously, one of the main classes you entouer is NSManagedObjectContext. I am unclear about the exact role of this. From the articles i've read, it seems that you can have multiple NSManagedObjectContexts. Does this mean that NSManagedObjectContext is basically a copy of the backend?
How would this resolve into a consistent backend when there is multiple different copies lying around?
So, 2 questions basically:
Is NSManagedContext a copy of the backend database?
and...
For example, say I make a change in context A and make some other change in context B. Then I call save on A first, then B? will B prevail?
Thanks
The NSManagedObjectContext is not a copy of the backend database. The documentation describes it as a scratch pad
An instance of NSManagedObjectContext represents a single “object
space” or scratch pad in an application. Its primary responsibility is
to manage a collection of managed objects. These objects form a group
of related model objects that represent an internally consistent view
of one or more persistent stores. A single managed object instance
exists in one and only one context, but multiple copies of an object
can exist in different contexts. Thus object uniquing is scoped to a
particular context.
The NSManagedObjectContext is just a temporary place to make changes to your managed objects in a transactional way. When you make changes to objects in a context it does not effect the backend database until and if you save the context, and as you know you can have multiple context that you can make changes to which is really important for concurrency.
For question number 2, the answer for who prevails will depend on the merge policy you set for your context and which one is called last which would be B. Here are the merge policies that can be set that will effect the second context to be saved.
NSErrorMergePolicyType
Specifies a policy that causes a save to fail
if there are any merge conflicts.
NSMergeByPropertyStoreTrumpMergePolicyType
Specifies a policy that
merges conflicts between the persistent store’s version of the object
and the current in-memory version, giving priority to external
changes.
NSMergeByPropertyObjectTrumpMergePolicyType
Specifies a policy that merges conflicts between the persistent store’s version
of the object and the current in-memory version, giving priority to
in-memory changes.
NSOverwriteMergePolicyType
Specifies a policy that
overwrites state in the persistent store for the changed objects in
conflict.
NSRollbackMergePolicyType
Specifies a policy that
discards in-memory state changes for objects in conflict.
An NSManagedObjectContext is specific representation of your data model. Each context maintains its own state (e.g. context) so changes in one context will not directly affect other contexts. When you work with multiple contexts it is your responsibility to keep them consistent by merging changes when a context saves its changes to the store.
Your question is regarding this process and may also involve merge conflicts. Whenever you save a context its changes are committed to the store and a merge policy is used to resolve conflicts.
When you save a context, it will post various notifications regarding progress. In your case, if [contextA save:&error] succeeds, the context will post the notification NSManagedObjectContextDidSaveNotification. When you have multiple contexts, you typically observe this notification and call:
[contextB mergeChangesFromContextDidSaveNotification:notification];
This will merge the changes saved on contextA into contextB.
EDIT: removed the 'thread-safe' comment. NSManagedObjectContext is not thread safe.

Core Data with multiple sqlite files

How may I use Core Data with multiple SQLite files?
Each file contains the same structures but the data is retrieved from different locations.
I want to be able to switch between these sqlite files at runtime based on application settings.
Sure; just point the persistent store coordinator (NSPersistentStoreCoordinator) at the different databases, as needed.
Persistent store coordinators support addition and removal of stores. On removal, you want to make sure that there are no unsaved changes in memory (obviously) and, in general, that you will not be messing with any entities fetched from the removed store after removal.
Be careful, though, as Core Data does not support relationships where the objects at either end are in different stores.