From 5a5c67e44523e9d8be270c435cce179a9a4c2334 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 7 Nov 2022 18:47:46 -0500 Subject: [PATCH] Try to prevent pruning accounts that still have statuses referencing them --- .../Tusker.xcdatamodeld/Tusker.xcdatamodel/contents | 12 +++--------- Tusker/Scenes/MainSceneDelegate.swift | 13 ++++++++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Tusker/CoreData/Tusker.xcdatamodeld/Tusker.xcdatamodel/contents b/Tusker/CoreData/Tusker.xcdatamodeld/Tusker.xcdatamodel/contents index f60d79ce..9ba83c7a 100644 --- a/Tusker/CoreData/Tusker.xcdatamodeld/Tusker.xcdatamodel/contents +++ b/Tusker/CoreData/Tusker.xcdatamodeld/Tusker.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -21,6 +21,7 @@ + @@ -84,7 +85,7 @@ - + @@ -92,11 +93,4 @@ - - - - - - - \ No newline at end of file diff --git a/Tusker/Scenes/MainSceneDelegate.swift b/Tusker/Scenes/MainSceneDelegate.swift index 0198d1ab..29642a73 100644 --- a/Tusker/Scenes/MainSceneDelegate.swift +++ b/Tusker/Scenes/MainSceneDelegate.swift @@ -125,12 +125,19 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate, TuskerSceneDelegate let statusReq: NSFetchRequest = StatusMO.fetchRequest() statusReq.predicate = NSPredicate(format: "(lastFetchedAt = nil) OR (lastFetchedAt < %@)", minDate as NSDate) let deleteStatusReq = NSBatchDeleteRequest(fetchRequest: statusReq) - _ = try? context.execute(deleteStatusReq) + deleteStatusReq.resultType = .resultTypeCount + if let res = try? context.execute(deleteStatusReq) as? NSBatchDeleteResult { + Logging.general.info("Pruned \(res.result as! Int) statuses") + } + let accountReq: NSFetchRequest = AccountMO.fetchRequest() - accountReq.predicate = NSPredicate(format: "(lastFetchedAt = nil) OR (lastFetchedAt < %@)", minDate as NSDate) + accountReq.predicate = NSPredicate(format: "((lastFetchedAt = nil) OR (lastFetchedAt < %@)) AND (statuses.@count = 0)", minDate as NSDate) let deleteAccountReq = NSBatchDeleteRequest(fetchRequest: accountReq) - _ = try? context.execute(deleteAccountReq) + deleteAccountReq.resultType = .resultTypeCount + if let res = try? context.execute(deleteAccountReq) as? NSBatchDeleteResult { + Logging.general.info("Pruned \(res.result as! Int) accounts") + } try? context.save() }