[AltStoreCore] Renames AsyncManaged.get() to perform()

Implies it can be used as alternative to managedObject.managedObjectContext.perform() and not just for retrieving values.
This commit is contained in:
Riley Testut
2023-05-11 14:41:01 -05:00
committed by Magesh K
parent 5b275d6811
commit a09f4bbd7a
2 changed files with 28 additions and 20 deletions

View File

@@ -355,7 +355,7 @@ extension AppManager
func add(@AsyncManaged _ source: Source, message: String? = nil, presentingViewController: UIViewController) async throws
{
let (sourceName, sourceURL) = await $source.get { ($0.name, $0.sourceURL) }
let (sourceName, sourceURL) = await $source.perform { ($0.name, $0.sourceURL) }
let context = DatabaseManager.shared.persistentContainer.newBackgroundContext()
async let fetchedSource = try await self.fetchSource(sourceURL: sourceURL, managedObjectContext: context) // Fetch source async while showing alert.
@@ -375,7 +375,7 @@ extension AppManager
func remove(@AsyncManaged _ source: Source, presentingViewController: UIViewController) async throws
{
let (sourceName, sourceID) = await $source.get { ($0.name, $0.identifier) }
let (sourceName, sourceID) = await $source.perform { ($0.name, $0.identifier) }
guard sourceID != Source.altStoreIdentifier else {
throw OperationError.forbidden(failureReason: NSLocalizedString("The default AltStore source cannot be removed.", comment: ""))
}

View File

@@ -34,10 +34,11 @@ public struct AsyncManaged<ManagedObject>
}
}
/// Run on managedObjectContext's queue.
public extension AsyncManaged
{
// Fetch multiple values.
func get<T>(_ closure: @escaping (ManagedObject) -> T) async -> T
// Non-throwing
func perform<T>(_ closure: @escaping (ManagedObject) -> T) async -> T
{
if let context = self.managedObjectContext
{
@@ -50,33 +51,40 @@ public extension AsyncManaged
return closure(self.wrappedValue)
}
}
// Throwing
func perform<T>(_ closure: @escaping (ManagedObject) throws -> T) async throws -> T
{
if let context = self.managedObjectContext
{
return try await context.performAsync {
try closure(self.wrappedValue)
}
}
else
{
return try closure(self.wrappedValue)
}
}
}
/// @dynamicMemberLookup
extension AsyncManaged
public extension AsyncManaged
{
public subscript<T>(dynamicMember keyPath: KeyPath<ManagedObject, T>) -> T {
subscript<T>(dynamicMember keyPath: KeyPath<ManagedObject, T>) -> T {
get async {
guard let context = self.managedObjectContext else {
return self.wrappedValue[keyPath: keyPath]
}
return await context.performAsync {
return self.wrappedValue[keyPath: keyPath]
}
let result = await self.perform { $0[keyPath: keyPath] }
return result
}
}
// Optionals
public subscript<Wrapped, T>(dynamicMember keyPath: KeyPath<Wrapped, T>) -> T? where ManagedObject == Optional<Wrapped> {
subscript<Wrapped, T>(dynamicMember keyPath: KeyPath<Wrapped, T>) -> T? where ManagedObject == Optional<Wrapped> {
get async {
guard let context = self.managedObjectContext else {
return self.wrappedValue?[keyPath: keyPath]
}
guard let wrappedValue else { return nil }
return await context.performAsync {
return self.wrappedValue?[keyPath: keyPath]
}
let result = await self.perform { _ in wrappedValue[keyPath: keyPath] }
return result
}
}
}