diff --git a/Shared/Extensions/NSManagedObjectContext+ExecuteAndMergeChanges.swift b/Shared/Extensions/NSManagedObjectContext+ExecuteAndMergeChanges.swift new file mode 100644 index 0000000..96bc4de --- /dev/null +++ b/Shared/Extensions/NSManagedObjectContext+ExecuteAndMergeChanges.swift @@ -0,0 +1,17 @@ +import CoreData + +extension NSManagedObjectContext { + /// Executes the given `NSBatchDeleteRequest` and directly merges the changes to bring the given + /// managed object context up to date. + /// + /// Credit: https://www.avanderlee.com/swift/nsbatchdeleterequest-core-data/ + /// + /// - Parameter batchDeleteRequest: The `NSBatchDeleteRequest` to execute. + /// - Throws: An error if anything went wrong executing the batch deletion. + public func executeAndMergeChanges(using batchDeleteRequest: NSBatchDeleteRequest) throws { + batchDeleteRequest.resultType = .resultTypeObjectIDs + let result = try execute(batchDeleteRequest) as? NSBatchDeleteResult + let changes: [AnyHashable: Any] = [NSDeletedObjectsKey: result?.result as? [NSManagedObjectID] ?? []] + NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [self]) + } +} diff --git a/Shared/LocalStorageManager.swift b/Shared/LocalStorageManager.swift index bd2b018..aa3e52f 100644 --- a/Shared/LocalStorageManager.swift +++ b/Shared/LocalStorageManager.swift @@ -50,9 +50,7 @@ class LocalStorageManager { let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) do { - try LocalStorageManager.persistentContainer.persistentStoreCoordinator.execute( - deleteRequest, with: LocalStorageManager.persistentContainer.viewContext - ) + try LocalStorageManager.persistentContainer.viewContext.executeAndMergeChanges(using: deleteRequest) } catch { print("Error: Failed to purge cached collections.") } diff --git a/Shared/Models/WriteFreelyModel.swift b/Shared/Models/WriteFreelyModel.swift index a440ea2..ebd56b6 100644 --- a/Shared/Models/WriteFreelyModel.swift +++ b/Shared/Models/WriteFreelyModel.swift @@ -174,8 +174,8 @@ private extension WriteFreelyModel { try purgeTokenFromKeychain(username: account.user?.username, server: account.server) client = nil DispatchQueue.main.async { - LocalStorageManager().purgeUserCollections() self.account.logout() + LocalStorageManager().purgeUserCollections() self.posts.purgeAllPosts() } } catch { diff --git a/Shared/PostList/PostListModel.swift b/Shared/PostList/PostListModel.swift index 5a32423..aab1ebd 100644 --- a/Shared/PostList/PostListModel.swift +++ b/Shared/PostList/PostListModel.swift @@ -28,9 +28,7 @@ class PostListModel: ObservableObject { let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) do { - try LocalStorageManager.persistentContainer.persistentStoreCoordinator.execute( - deleteRequest, with: LocalStorageManager.persistentContainer.viewContext - ) + try LocalStorageManager.persistentContainer.viewContext.executeAndMergeChanges(using: deleteRequest) } catch { print("Error: Failed to purge cached posts.") } diff --git a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj index 7fc3ef6..49a5016 100644 --- a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj +++ b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj @@ -54,6 +54,7 @@ 17C42E632507D8E600072984 /* PostStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C42E612507D8E600072984 /* PostStatus.swift */; }; 17C42E652509237800072984 /* PostListFilteredView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C42E642509237800072984 /* PostListFilteredView.swift */; }; 17C42E662509237800072984 /* PostListFilteredView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C42E642509237800072984 /* PostListFilteredView.swift */; }; + 17C42E70250AA12300072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C42E6F250AA12200072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift */; }; 17D435E824E3128F0036B539 /* PreferencesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D435E724E3128F0036B539 /* PreferencesModel.swift */; }; 17D435E924E3128F0036B539 /* PreferencesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D435E724E3128F0036B539 /* PreferencesModel.swift */; }; 17DF329D24C87D3500BCE2E3 /* Tests_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DF329C24C87D3500BCE2E3 /* Tests_iOS.swift */; }; @@ -114,6 +115,7 @@ 17B996D72502D23E0017B536 /* WFAPost+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WFAPost+CoreDataProperties.swift"; sourceTree = SOURCE_ROOT; }; 17C42E612507D8E600072984 /* PostStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostStatus.swift; sourceTree = ""; }; 17C42E642509237800072984 /* PostListFilteredView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostListFilteredView.swift; sourceTree = ""; }; + 17C42E6F250AA12200072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+ExecuteAndMergeChanges.swift"; sourceTree = ""; }; 17D435E724E3128F0036B539 /* PreferencesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesModel.swift; sourceTree = ""; }; 17DF328124C87D3300BCE2E3 /* WriteFreely_MultiPlatformApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteFreely_MultiPlatformApp.swift; sourceTree = ""; }; 17DF328224C87D3300BCE2E3 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -193,6 +195,7 @@ isa = PBXGroup; children = ( 1756AE8024CB844500FD7257 /* View+Keyboard.swift */, + 17C42E6F250AA12200072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift */, ); path = Extensions; sourceTree = ""; @@ -591,6 +594,7 @@ 17120DA324E19A42002B9F6C /* PreferencesView.swift in Sources */, 1756AE6E24CB255B00FD7257 /* PostListModel.swift in Sources */, 174D313224EC2831006CA9EE /* WriteFreelyModel.swift in Sources */, + 17C42E70250AA12300072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */, 17120DA124E19839002B9F6C /* AccountView.swift in Sources */, 1756AE7424CB26FA00FD7257 /* PostCellView.swift in Sources */, );