23 lines
491 B
Swift
23 lines
491 B
Swift
// SPDX-FileCopyrightText: 2026 Kaede Akino
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
import SwiftData
|
|
|
|
@MainActor
|
|
extension ModelContext {
|
|
public func savePendingChanges() throws {
|
|
try commit {}
|
|
}
|
|
|
|
public func commit(beforeSave: () throws -> Void = {}, _ operation: () throws -> Void) throws {
|
|
do {
|
|
try operation()
|
|
try beforeSave()
|
|
try save()
|
|
} catch {
|
|
rollback()
|
|
throw error
|
|
}
|
|
}
|
|
}
|