Home > database >  How to check in which persistent store a managed object stored?
How to check in which persistent store a managed object stored?

Time:10-12

My app uses core data to store data and it has multiple configurations: One configuration to sync with iCloud and other one for local data.

I did it by creating 2 NSPersistentStoreDescription, then assign them to the containter:

container.persistentStoreDescriptions = [cloud, local]

Those 2 persistent stores use the same model and entities. If I want to add a managedObject to a store, I uses this code after inserting and before saving context.

assignObject:toPersistentStore:

Problem is in a list of all fetched objects in both stores, is there a way to know in which persistent store a specific managed object stored?

CodePudding user response:

If you assign an entity to several configurations in your model file, the entity will be saved to all persistent stores whose descriptions have one of these configurations. In your case, both the local store and the cloud store will store new objects.

CodePudding user response:

for anyone who is interested: You can use this line to get persistent store or even configuration name:

managedObject.objectID.persistentStore?.configurationName 

Many thanks to joro_estropia from reddit for this answer

  • Related