Skip to content

Commit 2e03171

Browse files
committed
Add cusotm method for performing arbitrary operations against a scratchpad context
feature/add-method-for-custom-operations-in-context
1 parent 6ebf5f2 commit 2e03171

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// CoreDataRepository+Custom.swift
2+
// CoreDataRepository
3+
//
4+
//
5+
// MIT License
6+
//
7+
// Copyright © 2024 Andrew Roan
8+
9+
import CoreData
10+
import Foundation
11+
12+
extension CoreDataRepository {
13+
/// Escape hatch method for performing arbitrary operations inside a 'scratchpad' `NSManagedObjectContext` where
14+
/// changes will be discarded if not saved.
15+
public func custom<T>(
16+
schedule: NSManagedObjectContext.ScheduledTaskType = .enqueued,
17+
block: @escaping (
18+
_ parentContext: NSManagedObjectContext,
19+
_ scratchPadContext: NSManagedObjectContext
20+
) throws -> T
21+
) async -> Result<T, CoreDataError> {
22+
await context.performInScratchPad(schedule: schedule) { [context] scratchPad in try block(context, scratchPad) }
23+
}
24+
}

0 commit comments

Comments
 (0)