Skip to content

Commit

Permalink
Fix preventing db add from prev commit when some objects failed to fe…
Browse files Browse the repository at this point in the history
…tch inode
  • Loading branch information
danpashin committed Jul 6, 2024
1 parent 53a90ac commit 35c19ee
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion twackup-gui/Twackup/Sources/Models/Database/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ actor Database {
var index = 0
let total = packages.count

var failedIds = [NSManagedObjectID]()

let request = NSBatchInsertRequest(entity: DebPackageObject.entity()) { (object: NSManagedObject) in
guard index < total else { return true }

Expand All @@ -64,7 +66,10 @@ actor Database {
do {
try object.fillFrom(file: package.debURL)
} catch {
return true
// This branch must never be executed
// But if it is - remove object from database and remove deb since they are managed by db
failedIds.append(object.objectID)
try? FileManager.default.removeItem(at: package.debURL)
}
}

Expand All @@ -73,6 +78,11 @@ actor Database {
}

_ = await execute(request: request, context: backroundContext)

if !failedIds.isEmpty {
let request = NSBatchDeleteRequest(objectIDs: failedIds)
_ = await execute(request: request, context: backroundContext)
}
}

func fetchPackages() async throws -> [DebPackage] {
Expand Down

0 comments on commit 35c19ee

Please sign in to comment.