Skip to content

Commit

Permalink
Remove deb in database model since it should manage removal
Browse files Browse the repository at this point in the history
  • Loading branch information
danpashin committed Jun 16, 2024
1 parent 969d44e commit c5f23c3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
16 changes: 10 additions & 6 deletions twackup-gui/Twackup/Sources/Models/Database/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ actor Database {

func fetchPackages() throws -> [DebPackage] {
let request = DebPackageObject.fetchRequest()
return try backroundContext.fetch(request).map({ DebPackage(object: $0) })
return try backroundContext.fetch(request).map { DebPackage(object: $0) }
}

func fetch(package: Package) throws -> DebPackage? {
Expand All @@ -81,21 +81,25 @@ actor Database {
return DebPackage(object: object)
}

func delete(package: DebPackage) async {
await delete(packages: [package])
func delete(package: DebPackage) async throws {
try await delete(packages: [package])
}

func delete(packages: [DebPackage]) async {
func delete(packages: [DebPackage]) async throws {
if packages.isEmpty {
return
}

for pkg in packages {
try FileManager.default.removeItem(at: pkg.fileURL)
}

let request = NSBatchDeleteRequest(objectIDs: packages.map { $0.databaseID })
_ = await execute(request: request, context: backroundContext)
}

func delete(packages: [Package]) async {
await delete(packages: packages.compactMap { $0.asDEB })
func delete(packages: [Package]) async throws {
try await delete(packages: packages.compactMap { $0.asDEB })
}

func packagesSize() -> Int64 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import CoreData

final class DebPackage: Package, Sendable {

let databaseID: NSManagedObjectID

let id: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ class DatabasePackageProvider: PackageDataProvider, @unchecked Sendable {
!toDelete.contains { $0.isEqualTo(package) }
}

for package in toDelete {
guard let dbPackage = package.asDEB else { continue }
do {
try FileManager.default.removeItem(at: dbPackage.fileURL)
} catch {
await FFILogger.shared.log(error.localizedDescription, level: .warning)
}
do {
try await database.delete(packages: toDelete)
} catch {
await FFILogger.shared.log(error.localizedDescription, level: .warning)
return false
}

await database.delete(packages: toDelete)
applyFilter(currentFilter)

return true
Expand Down

0 comments on commit c5f23c3

Please sign in to comment.