Created
March 12, 2025 16:41
-
-
Save policante/c3db17c27d942cdbefee5c07ff8726c7 to your computer and use it in GitHub Desktop.
Classe de configuração do Persistence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersistenceController: NSObject, ObservableObject { | |
static let shared = PersistenceController() | |
private let ckContainerID = "iCloud.com.devpoli.sample" | |
private let appGroupContainerID = "group.com.devpoli.sample" | |
lazy var persistentContainer: NSPersistentCloudKitContainer = { | |
guard let modelURL = Bundle.main.url(forResource: "Sample", withExtension: "momd") else { | |
fatalError("Unable to find data model in the bundle.") | |
} | |
guard let coreDataModel = NSManagedObjectModel(contentsOf: modelURL) else { | |
fatalError("Unable to create the Core Data model.") | |
} | |
guard let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupContainerID) else { | |
fatalError("Unable to find App Group container") | |
} | |
let storeFolderUrl = appGroupURL.appending(component: "dataStore") | |
let sharedStoreFolderUrl = storeFolderUrl.appending(path: "Shared") | |
let fileManager = FileManager.default | |
for folder in [sharedStoreFolderUrl] where !fileManager.fileExists(atPath: folder.path()) { | |
do { | |
try fileManager.createDirectory(at: folder, withIntermediateDirectories: true) | |
} catch { | |
fatalError("#\(#function): Failed to create the store folder: \(error)") | |
} | |
} | |
let container = NSPersistentCloudKitContainer(name: "Sample") | |
guard let privateStoreDescription = container.persistentStoreDescriptions.first else { | |
fatalError("#\(#function): Failed to retrieve a persistent store description.") | |
} | |
privateStoreDescription.url = appGroupURL.appending(component: "Sample.sqlite") | |
privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) | |
privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) | |
let cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: ckContainerID) | |
cloudKitContainerOptions.databaseScope = .private | |
privateStoreDescription.cloudKitContainerOptions = cloudKitContainerOptions | |
guard let sharedStoreDescription = privateStoreDescription.copy() as? NSPersistentStoreDescription else { | |
fatalError("#\(#function): Copying the private store description returned an unexpected value.") | |
} | |
sharedStoreDescription.url = sharedStoreFolderUrl.appending(component: "SampleShared.sqlite") | |
let sharedContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: ckContainerID) | |
sharedContainerOptions.databaseScope = .shared | |
sharedStoreDescription.cloudKitContainerOptions = sharedContainerOptions | |
container.persistentStoreDescriptions.append(sharedStoreDescription) | |
self._sharedPersistentStore = nil | |
self._privatePersistentStore = nil | |
container.loadPersistentStores { store, error in | |
if let error = error as? NSError { | |
fatalError("Unresolved error \(error), \(error.userInfo)") | |
} | |
guard let cloudKitContainerOptions = store.cloudKitContainerOptions else { | |
return | |
} | |
if cloudKitContainerOptions.databaseScope == .private { | |
self._privatePersistentStore = container.persistentStoreCoordinator.persistentStore(for: store.url!) | |
} else if cloudKitContainerOptions.databaseScope == .shared { | |
self._sharedPersistentStore = container.persistentStoreCoordinator.persistentStore(for: store.url!) | |
} | |
} | |
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy | |
container.viewContext.transactionAuthor = TransactionAuthor.app | |
container.viewContext.automaticallyMergesChangesFromParent = true | |
do { | |
try container.viewContext.setQueryGenerationFrom(.current) | |
} catch { | |
fatalError("#\(#function): Failed to pin viewContext to the current generation:\(error)") | |
} | |
return container | |
}() | |
private var _privatePersistentStore: NSPersistentStore? | |
var privatePersistentStore: NSPersistentStore { | |
return _privatePersistentStore! | |
} | |
private var _sharedPersistentStore: NSPersistentStore? | |
var sharedPersistentStore: NSPersistentStore { | |
return _sharedPersistentStore! | |
} | |
lazy var cloudKitContainer: CKContainer = { | |
CKContainer(identifier: ckContainerID) | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment