Core Data: Using local and iCloud

Hi everyone,

I'm trying to use in my app core data as persistent storage. In this app it is supposed to have some entities of core data local but also some other synced through iCloud.

In the xcdatamodeld, I create two configs that belongs to each type offline(local) and the online(iCloud) but I'm getting a strange error where it says that it can't use the same container name. I don't understand if for each entity I want to be synced on iCloud I need a different container.

Also, I do not understand because if I use only iCloud syncing without using any configuration, I can store different entities.

Also, if each entity has a 1:1 relationship with the container, why is impossible to delete it, it doesn't make sense.

Here is the code I'm using
Code Block swift
persistentContainer = NSPersistentCloudKitContainer(name: name, managedObjectModel: model)
var defaultDirectoryURL: URL
var descriptors = [NSPersistentStoreDescription]()
for capability in self.dbCapabilities {
if capability.cloudEnable {
defaultDirectoryURL = NSPersistentCloudKitContainer.defaultDirectoryURL()
} else {
defaultDirectoryURL = NSPersistentContainer.defaultDirectoryURL()
}
let url = defaultDirectoryURL.appendingPathComponent(capability.url)
let descriptor = NSPersistentStoreDescription(url: url)           
descriptor.configuration = capability.configName
if capability.cloudEnable {
descriptor.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.\(capability.url)")
}
descriptors.append(descriptor)
}
persistentContainer.persistentStoreDescriptions = descriptors






Core Data: Using local and iCloud
 
 
Q