2021-01-27 07:50:13 +01:00
|
|
|
//
|
|
|
|
// CoreDataStack.swift
|
|
|
|
// CoreDataStack
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-1-27.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os
|
|
|
|
import Foundation
|
2021-07-16 10:21:47 +02:00
|
|
|
import Combine
|
2023-03-01 14:58:12 +01:00
|
|
|
@_exported import CoreData
|
2022-01-28 05:09:28 +01:00
|
|
|
import MastodonCommon
|
2021-01-27 07:50:13 +01:00
|
|
|
|
|
|
|
public final class CoreDataStack {
|
|
|
|
|
|
|
|
private(set) var storeDescriptions: [NSPersistentStoreDescription]
|
2021-07-16 10:21:47 +02:00
|
|
|
public let didFinishLoad = CurrentValueSubject<Bool, Never>(false)
|
2021-01-27 07:50:13 +01:00
|
|
|
|
|
|
|
init(persistentStoreDescriptions storeDescriptions: [NSPersistentStoreDescription]) {
|
|
|
|
self.storeDescriptions = storeDescriptions
|
|
|
|
}
|
|
|
|
|
2023-10-11 15:38:31 +02:00
|
|
|
public convenience init(databaseName: String = "shared", isInMemory: Bool = false) {
|
2021-04-27 10:26:59 +02:00
|
|
|
let storeURL = URL.storeURL(for: AppName.groupID, databaseName: databaseName)
|
2023-06-05 15:53:27 +02:00
|
|
|
let storeDescription: NSPersistentStoreDescription
|
|
|
|
if isInMemory {
|
|
|
|
storeDescription = NSPersistentStoreDescription(url: URL(string: "file:///dev/null")!) /// in-memory store with all features in favor of NSInMemoryStoreType
|
|
|
|
} else {
|
|
|
|
storeDescription = NSPersistentStoreDescription(url: storeURL)
|
|
|
|
}
|
2021-01-27 07:50:13 +01:00
|
|
|
self.init(persistentStoreDescriptions: [storeDescription])
|
|
|
|
}
|
|
|
|
|
|
|
|
public private(set) lazy var persistentContainer: NSPersistentContainer = {
|
|
|
|
/*
|
|
|
|
The persistent container for the application. This implementation
|
|
|
|
creates and returns a container, having loaded the store for the
|
|
|
|
application to it. This property is optional since there are legitimate
|
|
|
|
error conditions that could cause the creation of the store to fail.
|
|
|
|
*/
|
|
|
|
let container = CoreDataStack.persistentContainer()
|
|
|
|
CoreDataStack.configure(persistentContainer: container, storeDescriptions: storeDescriptions)
|
2021-07-16 10:21:47 +02:00
|
|
|
CoreDataStack.load(persistentContainer: container) { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.didFinishLoad.value = true
|
|
|
|
}
|
2021-01-27 07:50:13 +01:00
|
|
|
|
|
|
|
return container
|
|
|
|
}()
|
|
|
|
|
|
|
|
static func persistentContainer() -> NSPersistentContainer {
|
2022-01-28 05:09:28 +01:00
|
|
|
let bundles = [Bundle.module] // .module required for package in the SwiftPM
|
2021-01-27 07:50:13 +01:00
|
|
|
guard let managedObjectModel = NSManagedObjectModel.mergedModel(from: bundles) else {
|
|
|
|
fatalError("cannot locate bundles")
|
|
|
|
}
|
|
|
|
|
|
|
|
let container = NSPersistentContainer(name: "CoreDataStack", managedObjectModel: managedObjectModel)
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
|
|
|
|
static func configure(persistentContainer container: NSPersistentContainer, storeDescriptions: [NSPersistentStoreDescription]) {
|
|
|
|
container.persistentStoreDescriptions = storeDescriptions
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:21:47 +02:00
|
|
|
static func load(persistentContainer container: NSPersistentContainer, callback: @escaping () -> Void) {
|
2021-01-27 07:50:13 +01:00
|
|
|
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
|
|
|
|
if let error = error as NSError? {
|
|
|
|
// Replace this implementation with code to handle the error appropriately.
|
|
|
|
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
|
|
|
|
|
|
|
/*
|
|
|
|
Typical reasons for an error here include:
|
|
|
|
* The parent directory does not exist, cannot be created, or disallows writing.
|
|
|
|
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
|
|
|
|
* The device is out of space.
|
|
|
|
* The store could not be migrated to the current model version.
|
|
|
|
Check the error message to determine what the actual problem was.
|
|
|
|
*/
|
|
|
|
if let reason = error.userInfo["reason"] as? String,
|
|
|
|
(reason == "Can't find mapping model for migration" || reason == "Persistent store migration failed, missing mapping model.") {
|
|
|
|
if let storeDescription = container.persistentStoreDescriptions.first, let url = storeDescription.url {
|
|
|
|
try? container.persistentStoreCoordinator.destroyPersistentStore(at: url, ofType: NSSQLiteStoreType, options: nil)
|
|
|
|
} else {
|
|
|
|
assertionFailure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fatalError("Unresolved error \(error), \(error.userInfo)")
|
|
|
|
}
|
|
|
|
|
|
|
|
container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
|
|
|
|
|
|
|
|
// it's looks like the remote notification only trigger when app enter and leave background
|
|
|
|
container.viewContext.automaticallyMergesChangesFromParent = true
|
2023-09-21 12:52:05 +02:00
|
|
|
|
2021-07-16 10:21:47 +02:00
|
|
|
callback()
|
2021-01-27 07:50:13 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
extension CoreDataStack {
|
|
|
|
public func newTaskContext() -> NSManagedObjectContext {
|
|
|
|
let taskContext = persistentContainer.newBackgroundContext()
|
|
|
|
taskContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
|
|
|
|
taskContext.undoManager = nil
|
|
|
|
return taskContext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 15:53:27 +02:00
|
|
|
public extension CoreDataStack {
|
|
|
|
func tearDown() {
|
2021-01-27 07:50:13 +01:00
|
|
|
let oldStoreURL = persistentContainer.persistentStoreCoordinator.url(for: persistentContainer.persistentStoreCoordinator.persistentStores.first!)
|
|
|
|
try! persistentContainer.persistentStoreCoordinator.destroyPersistentStore(at: oldStoreURL, ofType: NSSQLiteStoreType, options: nil)
|
2023-06-05 15:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func rebuild() {
|
|
|
|
tearDown()
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-07-16 10:21:47 +02:00
|
|
|
CoreDataStack.load(persistentContainer: persistentContainer) { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.didFinishLoad.value = true
|
|
|
|
}
|
2021-01-27 07:50:13 +01:00
|
|
|
}
|
|
|
|
}
|