From fd6f3ed2bc8f83428cdd5e31ef8313e9af862b07 Mon Sep 17 00:00:00 2001 From: Yang Xu Date: Fri, 12 Aug 2022 14:59:17 +0800 Subject: [PATCH] fix a bug of delete transaction --- Sources/PersistentHistoryTrackingKit/Cleaner.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/PersistentHistoryTrackingKit/Cleaner.swift b/Sources/PersistentHistoryTrackingKit/Cleaner.swift index 99bef41..dc4cfca 100644 --- a/Sources/PersistentHistoryTrackingKit/Cleaner.swift +++ b/Sources/PersistentHistoryTrackingKit/Cleaner.swift @@ -29,19 +29,23 @@ struct Cleaner: TransactionCleanerProtocol { func cleanTransaction(before timestamp: Date?) throws { guard let timestamp = timestamp else { return } try backgroundContext.performAndWait { - let request = getPersistentStoreRequest(before: timestamp, for: authors) - try backgroundContext.execute(request) + if let request = getPersistentStoreRequest(before: timestamp, for: authors) { + try backgroundContext.execute(request) + } } } // make a request for delete transactions before timestamp - func getPersistentStoreRequest(before timestamp: Date, for allAuthors: [String]) -> NSPersistentStoreRequest { + func getPersistentStoreRequest(before timestamp: Date, for allAuthors: [String]) -> NSPersistentStoreRequest? { let historyStoreRequest = NSPersistentHistoryChangeRequest.deleteHistory(before: timestamp) if let fetchRequest = NSPersistentHistoryTransaction.fetchRequest { fetchRequest.predicate = createPredicateForAllAuthors(allAuthors: authors) historyStoreRequest.fetchRequest = fetchRequest + return historyStoreRequest + } + else { + return nil } - return historyStoreRequest } /// create predicate for all authors