|
| 1 | +import CoreData |
| 2 | + |
| 3 | +public final class PersistentContainer { |
| 4 | + public init( |
| 5 | + managedObjectContext: @escaping () throws -> NSManagedObjectContext, |
| 6 | + logError: ((Swift.Error) -> Void)? = nil, |
| 7 | + cleanUpAfterExecution: Bool = true |
| 8 | + ) { |
| 9 | + self.managedObjectContext = managedObjectContext |
| 10 | + self.logError = logError |
| 11 | + self.cleanUpAfterExecution = cleanUpAfterExecution |
| 12 | + } |
| 13 | + |
| 14 | + public enum Error: Swift.Error { |
| 15 | + case noResult |
| 16 | + } |
| 17 | + |
| 18 | + @discardableResult |
| 19 | + public func perform<T>( |
| 20 | + action: @escaping (ManagedObjectContext) throws -> T |
| 21 | + ) throws -> T { |
| 22 | + do { |
| 23 | + let context = try self.managedObjectContext() |
| 24 | + let reset = self.cleanUpAfterExecution |
| 25 | + |
| 26 | + if #available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) { |
| 27 | + return try context.performAndWait { |
| 28 | + try context.execute(reset, action) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + var result: Result<T, Swift.Error>? |
| 33 | + |
| 34 | + context.performAndWait { |
| 35 | + result = Result(catching: { |
| 36 | + try context.execute(reset, action) |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + switch result { |
| 41 | + case let .success(value): |
| 42 | + return value |
| 43 | + case let .failure(error): |
| 44 | + throw error |
| 45 | + case .none: |
| 46 | + throw Self.Error.noResult |
| 47 | + } |
| 48 | + } catch { |
| 49 | + self.logError?(error) |
| 50 | + |
| 51 | + throw error |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + #if canImport(_Concurrency) |
| 56 | + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 57 | + @discardableResult |
| 58 | + public func schedule<T>( |
| 59 | + action: @escaping (ManagedObjectContext) throws -> T |
| 60 | + ) async throws -> T { |
| 61 | + do { |
| 62 | + let context = try self.managedObjectContext() |
| 63 | + let reset = self.cleanUpAfterExecution |
| 64 | + |
| 65 | + if #available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) { |
| 66 | + return try await context.perform(schedule: .immediate) { |
| 67 | + try context.execute(reset, action) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + #if DEBUG |
| 72 | + return try await withCheckedThrowingContinuation { continuation in |
| 73 | + context.perform { |
| 74 | + continuation.resume(with: .init(catching: { |
| 75 | + try context.execute(reset, action) |
| 76 | + })) |
| 77 | + } |
| 78 | + } |
| 79 | + #else |
| 80 | + return try await withUnsafeThrowingContinuation { continuation in |
| 81 | + context.perform { |
| 82 | + continuation.resume(with: .init(catching: { |
| 83 | + try context.execute(reset, action) |
| 84 | + })) |
| 85 | + } |
| 86 | + } |
| 87 | + #endif |
| 88 | + } catch { |
| 89 | + self.logError?(error) |
| 90 | + |
| 91 | + throw error |
| 92 | + } |
| 93 | + } |
| 94 | + #endif |
| 95 | + |
| 96 | + private let managedObjectContext: () throws -> NSManagedObjectContext |
| 97 | + private let logError: ((Swift.Error) -> Void)? |
| 98 | + private let cleanUpAfterExecution: Bool |
| 99 | +} |
0 commit comments