mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 06:43:25 +01:00
[AltStoreCore] Adds NSManagedObjectContext.performAsync() to wrap iOS 15+ async perform()
Allows us to use Swift Concurrency with Core Data pre-iOS 15.
This commit is contained in:
61
AltStoreCore/Extensions/AltStore+Async.swift
Normal file
61
AltStoreCore/Extensions/AltStore+Async.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// AltStore+Async.swift
|
||||
// AltStoreCore
|
||||
//
|
||||
// Created by Riley Testut on 3/23/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import CoreData
|
||||
|
||||
public extension NSManagedObjectContext
|
||||
{
|
||||
// Non-Throwing
|
||||
func performAsync<T>(_ closure: @escaping () -> T) async -> T
|
||||
{
|
||||
let result: T
|
||||
|
||||
if #available(iOS 15, *)
|
||||
{
|
||||
result = await self.perform(schedule: .immediate) {
|
||||
closure()
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await withCheckedContinuation { (continuation: CheckedContinuation<T, Never>) in
|
||||
self.perform {
|
||||
let result = closure()
|
||||
continuation.resume(returning: result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Throwing
|
||||
func performAsync<T>(_ closure: @escaping () throws -> T) async throws -> T
|
||||
{
|
||||
let result: T
|
||||
|
||||
if #available(iOS 15, *)
|
||||
{
|
||||
result = try await self.perform(schedule: .immediate) {
|
||||
try closure()
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<T, Error>) in
|
||||
self.perform {
|
||||
let result = Result { try closure() }
|
||||
continuation.resume(with: result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user