1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-25 00:13:28 +01:00
mastodon-app-ufficiale-ipho.../CoreDataStack/Extension/NSManagedObjectContext.swift
2021-01-27 14:50:13 +08:00

51 lines
1.4 KiB
Swift

//
// NSManagedObjectContext.swift
// CoreDataStack
//
// Created by Cirno MainasuK on 2020-8-10.
// Copyright © 2020 Dimension. All rights reserved.
//
import os
import Foundation
import Combine
import CoreData
extension NSManagedObjectContext {
public func insert<T: NSManagedObject>() -> T where T: Managed {
guard let object = NSEntityDescription.insertNewObject(forEntityName: T.entityName, into: self) as? T else {
fatalError("cannot insert object: \(T.self)")
}
return object
}
public func saveOrRollback() throws {
do {
guard hasChanges else {
return
}
try save()
} catch {
rollback()
os_log("%{public}s[%{public}ld], %{public}s: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
throw error
}
}
public func performChanges(block: @escaping () -> Void) -> Future<Result<Void, Error>, Never> {
Future { promise in
self.perform {
block()
do {
try self.saveOrRollback()
promise(.success(Result.success(())))
} catch {
promise(.success(Result.failure(error)))
}
}
}
}
}