|
@@ -131,14 +131,14 @@ class CoreDataStack: ObservableObject {
|
|
|
do {
|
|
do {
|
|
|
try await fetchPersistentHistoryTransactionsAndChanges()
|
|
try await fetchPersistentHistoryTransactionsAndChanges()
|
|
|
} catch {
|
|
} catch {
|
|
|
- debugPrint("\(error.localizedDescription)")
|
|
|
|
|
|
|
+ debug(.coreData, "\(error.localizedDescription)")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private func fetchPersistentHistoryTransactionsAndChanges() async throws {
|
|
private func fetchPersistentHistoryTransactionsAndChanges() async throws {
|
|
|
let taskContext = newTaskContext()
|
|
let taskContext = newTaskContext()
|
|
|
taskContext.name = "persistentHistoryContext"
|
|
taskContext.name = "persistentHistoryContext"
|
|
|
-// debugPrint("Start fetching persistent history changes from the store ... \(DebuggingIdentifiers.inProgress)")
|
|
|
|
|
|
|
+// debug(.coreData,"Start fetching persistent history changes from the store ... \(DebuggingIdentifiers.inProgress)")
|
|
|
|
|
|
|
|
try await taskContext.perform {
|
|
try await taskContext.perform {
|
|
|
// Execute the persistent history change since the last transaction
|
|
// Execute the persistent history change since the last transaction
|
|
@@ -153,7 +153,7 @@ class CoreDataStack: ObservableObject {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private func mergePersistentHistoryChanges(from history: [NSPersistentHistoryTransaction]) {
|
|
private func mergePersistentHistoryChanges(from history: [NSPersistentHistoryTransaction]) {
|
|
|
-// debugPrint("Received \(history.count) persistent history transactions")
|
|
|
|
|
|
|
+// debug(.coreData,"Received \(history.count) persistent history transactions")
|
|
|
// Update view context with objectIDs from history change request
|
|
// Update view context with objectIDs from history change request
|
|
|
/// - Tag: mergeChanges
|
|
/// - Tag: mergeChanges
|
|
|
let viewContext = persistentContainer.viewContext
|
|
let viewContext = persistentContainer.viewContext
|
|
@@ -175,14 +175,25 @@ class CoreDataStack: ObservableObject {
|
|
|
let deleteHistoryTokensRequest = NSPersistentHistoryChangeRequest.deleteHistory(before: date)
|
|
let deleteHistoryTokensRequest = NSPersistentHistoryChangeRequest.deleteHistory(before: date)
|
|
|
do {
|
|
do {
|
|
|
try taskContext.execute(deleteHistoryTokensRequest)
|
|
try taskContext.execute(deleteHistoryTokensRequest)
|
|
|
- debugPrint("\(DebuggingIdentifiers.succeeded) Successfully deleted persistent history before \(date)")
|
|
|
|
|
|
|
+ debug(.coreData, "\(DebuggingIdentifiers.succeeded) Successfully deleted persistent history before \(date)")
|
|
|
} catch {
|
|
} catch {
|
|
|
- debugPrint(
|
|
|
|
|
|
|
+ debug(
|
|
|
|
|
+ .coreData,
|
|
|
"\(DebuggingIdentifiers.failed) Failed to delete persistent history before \(date): \(error.localizedDescription)"
|
|
"\(DebuggingIdentifiers.failed) Failed to delete persistent history before \(date): \(error.localizedDescription)"
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ func initializeStack() throws {
|
|
|
|
|
+ // Force initialization of persistent container
|
|
|
|
|
+ let container = persistentContainer
|
|
|
|
|
+
|
|
|
|
|
+ // Verify the store is loaded
|
|
|
|
|
+ guard container.persistentStoreCoordinator.persistentStores.isEmpty == false else {
|
|
|
|
|
+ throw CoreDataError.storeNotInitializedError(function: #function, file: #file)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Delete
|
|
// MARK: - Delete
|
|
@@ -192,7 +203,7 @@ extension CoreDataStack {
|
|
|
/// - Tag: synchronousDelete
|
|
/// - Tag: synchronousDelete
|
|
|
func deleteObject(identifiedBy objectID: NSManagedObjectID) async {
|
|
func deleteObject(identifiedBy objectID: NSManagedObjectID) async {
|
|
|
let viewContext = persistentContainer.viewContext
|
|
let viewContext = persistentContainer.viewContext
|
|
|
- debugPrint("Start deleting data from the store ...\(DebuggingIdentifiers.inProgress)")
|
|
|
|
|
|
|
+ debug(.coreData, "Start deleting data from the store ...\(DebuggingIdentifiers.inProgress)")
|
|
|
|
|
|
|
|
await viewContext.perform {
|
|
await viewContext.perform {
|
|
|
do {
|
|
do {
|
|
@@ -201,9 +212,9 @@ extension CoreDataStack {
|
|
|
|
|
|
|
|
guard viewContext.hasChanges else { return }
|
|
guard viewContext.hasChanges else { return }
|
|
|
try viewContext.save()
|
|
try viewContext.save()
|
|
|
- debugPrint("Successfully deleted data. \(DebuggingIdentifiers.succeeded)")
|
|
|
|
|
|
|
+ debug(.coreData, "Successfully deleted data. \(DebuggingIdentifiers.succeeded)")
|
|
|
} catch {
|
|
} catch {
|
|
|
- debugPrint("Failed to delete data: \(error.localizedDescription)")
|
|
|
|
|
|
|
+ debug(.coreData, "Failed to delete data: \(error.localizedDescription)")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -244,7 +255,7 @@ extension CoreDataStack {
|
|
|
|
|
|
|
|
// Guard check if there are NSManagedObjects older than the specified days
|
|
// Guard check if there are NSManagedObjects older than the specified days
|
|
|
guard !objectIDs.isEmpty else {
|
|
guard !objectIDs.isEmpty else {
|
|
|
-// debugPrint("No objects found older than \(days) days.")
|
|
|
|
|
|
|
+// debug(.coreData,"No objects found older than \(days) days.")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -255,14 +266,14 @@ extension CoreDataStack {
|
|
|
let batchDeleteResult = fetchResult as? NSBatchDeleteResult,
|
|
let batchDeleteResult = fetchResult as? NSBatchDeleteResult,
|
|
|
let success = batchDeleteResult.result as? Bool, success
|
|
let success = batchDeleteResult.result as? Bool, success
|
|
|
else {
|
|
else {
|
|
|
- debugPrint("Failed to execute batch delete request \(DebuggingIdentifiers.failed)")
|
|
|
|
|
|
|
+ debug(.coreData, "Failed to execute batch delete request \(DebuggingIdentifiers.failed)")
|
|
|
throw CoreDataError.batchDeleteError(function: callingFunction, file: callingClass)
|
|
throw CoreDataError.batchDeleteError(function: callingFunction, file: callingClass)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- debugPrint("Successfully deleted data older than \(days) days. \(DebuggingIdentifiers.succeeded)")
|
|
|
|
|
|
|
+ debug(.coreData, "Successfully deleted data older than \(days) days. \(DebuggingIdentifiers.succeeded)")
|
|
|
} catch {
|
|
} catch {
|
|
|
- debugPrint("Failed to fetch or delete data: \(error.localizedDescription) \(DebuggingIdentifiers.failed)")
|
|
|
|
|
|
|
+ debug(.coreData, "Failed to fetch or delete data: \(error.localizedDescription) \(DebuggingIdentifiers.failed)")
|
|
|
throw CoreDataError.unexpectedError(error: error, function: callingFunction, file: callingClass)
|
|
throw CoreDataError.unexpectedError(error: error, function: callingFunction, file: callingClass)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -294,7 +305,7 @@ extension CoreDataStack {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
guard !parentObjectIDs.isEmpty else {
|
|
guard !parentObjectIDs.isEmpty else {
|
|
|
-// debugPrint("No \(parentType) objects found older than \(days) days.")
|
|
|
|
|
|
|
+// debug(.coreData,"No \(parentType) objects found older than \(days) days.")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -308,7 +319,7 @@ extension CoreDataStack {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
guard !childObjectIDs.isEmpty else {
|
|
guard !childObjectIDs.isEmpty else {
|
|
|
-// debugPrint("No \(childType) objects found related to \(parentType) objects older than \(days) days.")
|
|
|
|
|
|
|
+// debug(.coreData,"No \(childType) objects found related to \(parentType) objects older than \(days) days.")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -319,16 +330,17 @@ extension CoreDataStack {
|
|
|
let batchDeleteResult = fetchResult as? NSBatchDeleteResult,
|
|
let batchDeleteResult = fetchResult as? NSBatchDeleteResult,
|
|
|
let success = batchDeleteResult.result as? Bool, success
|
|
let success = batchDeleteResult.result as? Bool, success
|
|
|
else {
|
|
else {
|
|
|
- debugPrint("Failed to execute batch delete request \(DebuggingIdentifiers.failed)")
|
|
|
|
|
|
|
+ debug(.coreData, "Failed to execute batch delete request \(DebuggingIdentifiers.failed)")
|
|
|
throw CoreDataError.batchDeleteError(function: callingFunction, file: callingClass)
|
|
throw CoreDataError.batchDeleteError(function: callingFunction, file: callingClass)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- debugPrint(
|
|
|
|
|
|
|
+ debug(
|
|
|
|
|
+ .coreData,
|
|
|
"Successfully deleted \(childType) data related to \(parentType) objects older than \(days) days. \(DebuggingIdentifiers.succeeded)"
|
|
"Successfully deleted \(childType) data related to \(parentType) objects older than \(days) days. \(DebuggingIdentifiers.succeeded)"
|
|
|
)
|
|
)
|
|
|
} catch {
|
|
} catch {
|
|
|
- debugPrint("Failed to fetch or delete data: \(error.localizedDescription) \(DebuggingIdentifiers.failed)")
|
|
|
|
|
|
|
+ debug(.coreData, "Failed to fetch or delete data: \(error.localizedDescription) \(DebuggingIdentifiers.failed)")
|
|
|
throw CoreDataError.unexpectedError(error: error, function: callingFunction, file: callingClass)
|
|
throw CoreDataError.unexpectedError(error: error, function: callingFunction, file: callingClass)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -479,7 +491,7 @@ extension CoreDataStack {
|
|
|
do {
|
|
do {
|
|
|
try context.save()
|
|
try context.save()
|
|
|
} catch {
|
|
} catch {
|
|
|
- debugPrint("Error saving context \(DebuggingIdentifiers.failed): \(error)")
|
|
|
|
|
|
|
+ debug(.coreData, "Error saving context \(DebuggingIdentifiers.failed): \(error)")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -495,11 +507,12 @@ extension NSManagedObjectContext {
|
|
|
do {
|
|
do {
|
|
|
guard onContext.hasChanges else { return }
|
|
guard onContext.hasChanges else { return }
|
|
|
try onContext.save()
|
|
try onContext.save()
|
|
|
-// debugPrint(
|
|
|
|
|
|
|
+// debug(.coreData,
|
|
|
// "Saving to Core Data successful in \(callingFunction) in \(callingClass): \(DebuggingIdentifiers.succeeded)"
|
|
// "Saving to Core Data successful in \(callingFunction) in \(callingClass): \(DebuggingIdentifiers.succeeded)"
|
|
|
// )
|
|
// )
|
|
|
} catch let error as NSError {
|
|
} catch let error as NSError {
|
|
|
- debugPrint(
|
|
|
|
|
|
|
+ debug(
|
|
|
|
|
+ .coreData,
|
|
|
"Saving to Core Data failed in \(callingFunction) in \(callingClass): \(DebuggingIdentifiers.failed) with error \(error), \(error.userInfo)"
|
|
"Saving to Core Data failed in \(callingFunction) in \(callingClass): \(DebuggingIdentifiers.failed) with error \(error), \(error.userInfo)"
|
|
|
)
|
|
)
|
|
|
throw error
|
|
throw error
|