From 61f073109c4b35b1d3dedb99680389765a76d851 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 9 Jan 2022 18:46:42 -0500 Subject: [PATCH] Fix CoreData concurrency things --- .../xcshareddata/xcschemes/Reader.xcscheme | 104 ++++++++++++++++++ Reader/CoreData/PersistentContainer.swift | 13 ++- 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 Reader.xcodeproj/xcshareddata/xcschemes/Reader.xcscheme diff --git a/Reader.xcodeproj/xcshareddata/xcschemes/Reader.xcscheme b/Reader.xcodeproj/xcshareddata/xcschemes/Reader.xcscheme new file mode 100644 index 0000000..1f10bed --- /dev/null +++ b/Reader.xcodeproj/xcshareddata/xcschemes/Reader.xcscheme @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Reader/CoreData/PersistentContainer.swift b/Reader/CoreData/PersistentContainer.swift index c5ed416..68727e6 100644 --- a/Reader/CoreData/PersistentContainer.swift +++ b/Reader/CoreData/PersistentContainer.swift @@ -39,6 +39,13 @@ class PersistentContainer: NSPersistentContainer { } } + @MainActor + private func saveViewContext() async throws { + if viewContext.hasChanges { + try viewContext.save() + } + } + func lastSyncDate() async throws -> Date? { return try await backgroundContext.perform { let state = try self.backgroundContext.fetch(SyncState.fetchRequest()).first @@ -56,8 +63,8 @@ class PersistentContainer: NSPersistentContainer { } try self.backgroundContext.save() - try self.viewContext.save() } + try await self.saveViewContext() } func sync(serverGroups: [Fervor.Group], serverFeeds: [Fervor.Feed]) async throws { @@ -87,9 +94,9 @@ class PersistentContainer: NSPersistentContainer { if self.backgroundContext.hasChanges { try self.backgroundContext.save() - try self.viewContext.save() } } + try await self.saveViewContext() } func syncItems(_ syncUpdate: ItemsSyncUpdate) async throws { @@ -123,9 +130,9 @@ class PersistentContainer: NSPersistentContainer { if self.backgroundContext.hasChanges { try self.backgroundContext.save() - try self.viewContext.save() } } + try await self.saveViewContext() } }