[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 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() let context = DatabaseManager.shared.persistentContainer.newBackgroundContext()
async let fetchedSource = try await self.fetchSource(sourceURL: sourceURL, managedObjectContext: context) // Fetch source async while showing alert. 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 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 { guard sourceID != Source.altStoreIdentifier else {
throw OperationError.forbidden(failureReason: NSLocalizedString("The default AltStore source cannot be removed.", comment: "")) 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 public extension AsyncManaged
{ {
// Fetch multiple values. // Non-throwing
func get<T>(_ closure: @escaping (ManagedObject) -> T) async -> T func perform<T>(_ closure: @escaping (ManagedObject) -> T) async -> T
{ {
if let context = self.managedObjectContext if let context = self.managedObjectContext
{ {
@@ -50,33 +51,40 @@ public extension AsyncManaged
return closure(self.wrappedValue) 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 /// @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 { get async {
guard let context = self.managedObjectContext else { let result = await self.perform { $0[keyPath: keyPath] }
return self.wrappedValue[keyPath: keyPath] return result
}
return await context.performAsync {
return self.wrappedValue[keyPath: keyPath]
}
} }
} }
// Optionals // 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 { get async {
guard let context = self.managedObjectContext else { guard let wrappedValue else { return nil }
return self.wrappedValue?[keyPath: keyPath]
}
return await context.performAsync { let result = await self.perform { _ in wrappedValue[keyPath: keyPath] }
return self.wrappedValue?[keyPath: keyPath] return result
}
} }
} }
} }