mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 06:43:25 +01:00
[AltStoreCore] Backports iOS 15+ NSManagedObjectContext.performAndWait<T>()
Simplifies returning values and throwing errors from managed object contexts.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// NSManagedObjectContext+Conveniences.swift
|
||||
// AltStore
|
||||
//
|
||||
// Created by Riley Testut on 5/16/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
|
||||
public extension NSManagedObjectContext
|
||||
{
|
||||
// Non-throwing
|
||||
func performAndWait<T>(_ closure: @escaping () -> T) -> T
|
||||
{
|
||||
var result: T!
|
||||
|
||||
self.performAndWait {
|
||||
result = closure()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Throwing
|
||||
func performAndWait<T>(_ closure: @escaping () throws -> T) throws -> T
|
||||
{
|
||||
var result: Result<T, Error>!
|
||||
|
||||
self.performAndWait {
|
||||
result = Result { try closure() }
|
||||
}
|
||||
|
||||
let value = try result.get()
|
||||
return value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user