Skip to content

Commit

Permalink
fix a bug of delete transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
fatbobman committed Aug 12, 2022
1 parent 269b2a6 commit fd6f3ed
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Sources/PersistentHistoryTrackingKit/Cleaner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fd6f3ed

Please sign in to comment.