Is it possible to use MULTIPLE `Persistent Store Coordinator` with ONE `Persistent Store File`? And how?

413 Views Asked by At

I came across this video very recently:

Core Data Performance Optimization and Debugging

In this video I saw this concept about using MULTIPLE Persistent Store Coordinator with ONE Persistent Store File.

If its of any help, here is a screenshot of the idea:

enter image description here

I have looked into this question:

How to create two persistent stores in one persistent store coordinator

I tried the solution given in the above question (without using the journal_mode as DELETE) and also the following:

managedObjectModel = modelAtUrl
let persistentStoreCoordinator1 = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
let persistentStoreCoordinator2 = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
...
...
queue = DispatchQueue(label: "DataStoreControllerSerialQueue")

    queue.async() {

let options = [
        NSMigratePersistentStoresAutomaticallyOption: true,
        NSInferMappingModelAutomaticallyOption: true
    ]

        do {
            try persistentStoreCoordinator1.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeUrl as URL, options: options)
        } catch let error as NSError {
            print("Unable to initialize persistent store coordinator:", error)
            self.error = error
        } catch {
            fatalError()
        }

do {
            try persistentStoreCoordinator2.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeUrl as URL, options: options)
        } catch let error as NSError {
            print("Unable to initialize persistent store coordinator:", error)
            self.error = error
        } catch {
            fatalError()
        }
    }


...

And I am getting all sorts of file I/O errors (error code 522), and also sometimes could not add the same store error.

So how do I go about it?

Is it even possible? Given that the NSPersistentStore has the following property NSPersistentStore -> persistentStoreCoordinator to return the associated coordinator (not coordinators).

P.S. There is no particular difficulty that I want to solve with this problem. So I am not looking for alternative background data task solutions. I am specifically interested to know how this implementation is possible (if it is possible at all?)

Apologies in advance if the question is badly explained or badly formatted. Please let me know if it needs more explaining.

Thanks in advance for helps.

0

There are 0 best solutions below