Does it make sense to have multiple NSPsistentStoreCoordinators?

I am new to Core Data and I try to have the correct data model and its use.

I basically have two types of files in my application ... one contains data like settings, and the second contains data sets that the user will work with (like similar documents, although I can imagine that the user works through 10 or even 100s of these files at the same time).

I read books on master data, and I remember how usually an application has one NSPersistentStoreCoordinator , one NSManagedObjectContext and one NSManagedObjectModel .

Currently, I have one managed object model with configurations for the various types of files that I have. I also planned to have one of NSPersistentStoreCoordinators / NSManagedObjectContexts , and when I create new Core Data objects, I would make sure that each of them was added to the correct persistent storage.

However, I saw examples where each file has its own NSPersistentStoreCoordinator and NSManagedObjectContext .

Are there any advantages and disadvantages to having multiple NSPersistentStoreCoordinators and NSManagedObjectContexts in the same thread application?

At first, I hoped that you could move objects from one persistent storage to another while editing the rights to cut and paste the user, but this is not possible in any case.

Any advice is appreciated!

Edit

Here is some more information about what bothers me. When I read the documentation on NSPsistentStoreCoordinator, it says:

The coordinator is designed to represent the facade of the managed object contexts, so that a group of permanent stores are displayed as an aggregate store.

In my case, this is not what I want. I want my documents to be perceived as separate documents, and I do not want requests to be confused with each other.

In addition, with one persistent repository coordinator and many persistent repositories, if I forget to assign an entity to the correct repository at creation, I find errors with errors, since entities are randomly assigned to the actual repository at their creation. I'm not sure what will happen to relationships that point to objects in different stores (maybe approval fail?).

It seems to me that having a single context coordinator / persistent storage in the repository will be less error prone and allows me to keep the data for each document isolated from each other.

The only thing that seems to be buying one permanent store is that I can perform a save operation for all stores at the same time, which would be preferable. With multiple context / storage coordinators, I will need to perform separate save operations.

If you use the OSX class NSPersistentDocument, it seems to use a separate context / storage coordinator for each document.

In any case, from all my research, it seems that separate repository coordinators / contexts will work better for my application, but the reason I posted this is because I'm new to Core Data, and this approach seems to go contrary to the recommended stream and I'm worried that I am missing some errors that will come back to bite me.

More thoughts / information.

As I think more about this and read more reviews from others (thanks everyone !!!), my current thoughts are as follows.

There really aren't many differences between the two approaches for me, and currently I believe that I could work well anyway.

With one repository coordinator, I need to make sure that the newly created objects are connected to the correct repository (not a big deal). With several storage coordinators, I need to make sure that the newly created objects are added to the correct context (of which I will have many of them). With the way my code is structured, any approach should be relatively easy for me.

I personally want to search one store at a time. If I have several store coordinators, this is automatic. If I have one repository coordinator, I must definitely limit the fetch request. (in any case, not a very big deal).

The documentation for the store coordinator implies that its advantage is that several stores are displayed as a unit. For my application, I don’t need or need it, so for me it’s not very important (although if I wanted to add cross-storage search capabilities in the future, it would be better to save everything in one store coordinator).

For me, none of the above reasons are really good arguments anyway, and if they were the only arguments, I would probably try to do something in a more ordinary way and stick to a single repository coordinator.

However, one of the last reasons (and the main reason I originally posted this question) is that I plan to use some features in iOS 5, which seem to require several storage coordinators. I want my app to have a weak connection in order to be backward compatible, so it seems that my iOS 4 code is very similar to iOS 5. It would be preferable.

The more I think about it, supporting several versions of the OS, I can probably still implement things anyway with the right abstractions.

Thank you all for your feedback! I'm slowly getting Core Data support, which was basically a great experience, although it provided me with my share of headaches!

+4
source share
3 answers

Typically, an application will use only one PersistentStoreCoordinator and is initialized in the application’s deletion.

For more information and clarification, please check the apple document on the Basic Data.

+2
source

I can’t honestly think about the reason why you would like to have a second coordinator if you are not performing quite serious parallel multi-threaded tasks on the same model. From what you described above, you may need to create a separate context for certain managed objects or, possibly, individual stores, if you need them completely independent.

You are unlikely to ever interact with a permanent store coordinator, since most of your operations are carried out at the context level, and then saved when you are ready (again through the context) through the store coordinator.

You obviously did your own research, so I'm not going to check the XYZ documentation for you (Core Data is well documented for a basic level, but something is a bit more advanced, and you are on your own), but my main idea is that having a separate store coordinator for each of these models is likely to increase the complexity of your code, rather than simplify management, which seems to be your main motivation in the original question.

+2
source

I do not see any real technical limitations on having multiple instances of NSPersistentStoreCoordinator in your application if they all point to a unique location on disk.

However, this approach is definitely not a common occurrence and will add a lot of complexity to your application, which may not be needed. From what you described about your data model, I see no reason why you would need several NSPersistentStoreCoordinators.

Be sure to read the CoreData Programming Guide and find out that you need to create a unique NSManagedObjectContext for the stream, not for the NSPersistentStoreCoordinator, as you described in your question.

+1
source

All Articles