[AltStoreCore] Backports iOS 15+ NSManagedObjectContext.performAndWait<T>()

Simplifies returning values and throwing errors from managed object contexts.
This commit is contained in:
Riley Testut
2023-05-16 15:15:43 -05:00
committed by Magesh K
parent bec6ca9eec
commit 177d453491
3 changed files with 50 additions and 9 deletions

View File

@@ -368,6 +368,7 @@
D57FE84428C7DB7100216002 /* ErrorLogViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57FE84328C7DB7100216002 /* ErrorLogViewController.swift */; };
D586D39B28EF58B0000E101F /* AltTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D586D39A28EF58B0000E101F /* AltTests.swift */; };
D58916FE28C7C55C00E39C8B /* LoggedError.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58916FD28C7C55C00E39C8B /* LoggedError.swift */; };
D5893F802A1419E800E767CD /* NSManagedObjectContext+Conveniences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5893F7E2A14183200E767CD /* NSManagedObjectContext+Conveniences.swift */; };
D58D5F2E26DFE68E00E55E38 /* LaunchAtLogin in Frameworks */ = {isa = PBXBuildFile; productRef = D58D5F2D26DFE68E00E55E38 /* LaunchAtLogin */; };
D59162AB29BA60A9005CBF47 /* SourceHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D59162AA29BA60A9005CBF47 /* SourceHeaderView.swift */; };
D59162AD29BA616A005CBF47 /* SourceHeaderView.xib in Resources */ = {isa = PBXBuildFile; fileRef = D59162AC29BA616A005CBF47 /* SourceHeaderView.xib */; };
@@ -929,6 +930,7 @@
D586D39828EF58B0000E101F /* AltTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AltTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D586D39A28EF58B0000E101F /* AltTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AltTests.swift; sourceTree = "<group>"; };
D58916FD28C7C55C00E39C8B /* LoggedError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggedError.swift; sourceTree = "<group>"; };
D5893F7E2A14183200E767CD /* NSManagedObjectContext+Conveniences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+Conveniences.swift"; sourceTree = "<group>"; };
D59162AA29BA60A9005CBF47 /* SourceHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceHeaderView.swift; sourceTree = "<group>"; };
D59162AC29BA616A005CBF47 /* SourceHeaderView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SourceHeaderView.xib; sourceTree = "<group>"; };
D5927D6529DCC89000D6898E /* UINavigationBarAppearance+TintColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UINavigationBarAppearance+TintColor.swift"; sourceTree = "<group>"; };
@@ -1539,6 +1541,8 @@
BF6A531F246DC1B0004F59C8 /* FileManager+SharedDirectories.swift */,
0E0502592BEC83C500879B5C /* OperatingSystemVersion+Comparable.swift */,
0E05025B2BEC947000879B5C /* String+SideStore.swift */,
D5F48B4729CCF21B002B52A4 /* AltStore+Async.swift */,
D5893F7E2A14183200E767CD /* NSManagedObjectContext+Conveniences.swift */,
);
path = Extensions;
sourceTree = "<group>";
@@ -2646,6 +2650,7 @@
D58916FE28C7C55C00E39C8B /* LoggedError.swift in Sources */,
BFBF331B2526762200B7B8C9 /* AltStore8ToAltStore9.xcmappingmodel in Sources */,
0EE7FDC72BE8CF4100D1E390 /* ALTWrappedError.m in Sources */,
D5893F802A1419E800E767CD /* NSManagedObjectContext+Conveniences.swift in Sources */,
D5CA0C4E280E249E00469595 /* AltStore9ToAltStore10.xcmappingmodel in Sources */,
D51AD27F29356B7B00967AAA /* ALTWrappedError.m in Sources */,
BF989184250AACFC002ACF50 /* Date+RelativeDate.swift in Sources */,

View File

@@ -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
}
}

View File

@@ -40,12 +40,12 @@ public extension Managed
// Non-throwing
func perform<T>(_ closure: @escaping (ManagedObject) -> T) -> T
{
var result: T!
let result: T
if let context = self.managedObjectContext
{
context.performAndWait {
result = closure(self.wrappedValue)
result = context.performAndWait {
closure(self.wrappedValue)
}
}
else
@@ -59,21 +59,20 @@ public extension Managed
// Throwing
func perform<T>(_ closure: @escaping (ManagedObject) throws -> T) throws -> T
{
var result: Result<T, Error>!
let result: T
if let context = self.managedObjectContext
{
context.performAndWait {
result = Result { try closure(self.wrappedValue) }
result = try context.performAndWait {
try closure(self.wrappedValue)
}
}
else
{
result = Result { try closure(self.wrappedValue) }
result = try closure(self.wrappedValue)
}
let value = try result.get()
return value
return result
}
}