Skip to content

Commit 3a7c68e

Browse files
committed
Merge branch '3.0-preview' into feature/add-inlinable-notations-to-encourage-generic-specialization
# Conflicts: # Sources/CoreDataRepository/CoreDataRepository+CRUD.swift # Sources/CoreDataRepository/NSManagedObject+Helpers.swift # Sources/CoreDataRepository/NSManagedObjectContext+Helpers.swift
2 parents 24df2e5 + 7a37be3 commit 3a7c68e

4 files changed

Lines changed: 37 additions & 21 deletions

File tree

Sources/CoreDataRepository/CoreDataRepository+CRUD.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ extension CoreDataRepository {
104104
let readContext = context.childContext()
105105
return AsyncStream { continuation in
106106
let provider: ReadSubscription<Model>
107-
switch Self.getObjectId(fromUrl: url, context: readContext) {
107+
switch readContext.objectId(from: url) {
108108
case let .success(objectId):
109109
provider = ReadSubscription<Model>(
110110
objectId: objectId,
@@ -132,7 +132,7 @@ extension CoreDataRepository {
132132
let readContext = context.childContext()
133133
return AsyncThrowingStream { continuation in
134134
let provider: ReadThrowingSubscription<Model>
135-
switch Self.getObjectId(fromUrl: url, context: readContext) {
135+
switch readContext.objectId(from: url) {
136136
case let .success(objectId):
137137
provider = ReadThrowingSubscription<Model>(
138138
objectId: objectId,
@@ -151,15 +151,4 @@ extension CoreDataRepository {
151151
}
152152
}
153153
}
154-
155-
@usableFromInline
156-
static func getObjectId(
157-
fromUrl url: URL,
158-
context: NSManagedObjectContext
159-
) -> Result<NSManagedObjectID, CoreDataError> {
160-
guard let objectId = context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: url) else {
161-
return Result.failure(.failedToGetObjectIdFromUrl(url))
162-
}
163-
return .success(objectId)
164-
}
165154
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
///
16+
/// The caller is responsible for saving the contexts and cleaning up if needed.
17+
/// All this method provides is the contexts and mapping `Error` into ``CoreDataError``.
18+
public func custom<T>(
19+
schedule: NSManagedObjectContext.ScheduledTaskType = .enqueued,
20+
block: @escaping (
21+
_ parentContext: NSManagedObjectContext,
22+
_ scratchPadContext: NSManagedObjectContext
23+
) throws -> T
24+
) async -> Result<T, CoreDataError> {
25+
await context.performInScratchPad(schedule: schedule) { [context] scratchPad in try block(context, scratchPad) }
26+
}
27+
}

Sources/CoreDataRepository/Internal/NSManagedObject+CRUDHelpers.swift renamed to Sources/CoreDataRepository/NSManagedObject+Helpers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// NSManagedObject+CRUDHelpers.swift
1+
// NSManagedObject+Helpers.swift
22
// CoreDataRepository
33
//
44
//
@@ -11,8 +11,8 @@ import Foundation
1111

1212
extension NSManagedObject {
1313
/// Helper function to handle casting ``NSManagedObject`` to a sub-class.
14-
@usableFromInline
15-
func asManagedModel<T>() throws -> T where T: NSManagedObject {
14+
@inlinable
15+
public func asManagedModel<T>() throws -> T where T: NSManagedObject {
1616
guard let repoManaged = self as? T else {
1717
throw CoreDataError.fetchedObjectFailedToCastToExpectedType
1818
}

Sources/CoreDataRepository/Internal/NSManagedObjectContext+CRUDHelpers.swift renamed to Sources/CoreDataRepository/NSManagedObjectContext+Helpers.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// NSManagedObjectContext+CRUDHelpers.swift
1+
// NSManagedObjectContext+Helpers.swift
22
// CoreDataRepository
33
//
44
//
@@ -11,17 +11,17 @@ import Foundation
1111

1212
extension NSManagedObjectContext {
1313
/// Helper function for getting the ``NSManagedObjectID`` from an ``URL``
14-
@usableFromInline
15-
func objectId(from url: URL) -> Result<NSManagedObjectID, CoreDataError> {
14+
@inlinable
15+
public func objectId(from url: URL) -> Result<NSManagedObjectID, CoreDataError> {
1616
guard let objectId = persistentStoreCoordinator?.managedObjectID(forURIRepresentation: url) else {
1717
return .failure(CoreDataError.failedToGetObjectIdFromUrl(url))
1818
}
1919
return .success(objectId)
2020
}
2121

2222
/// Helper function for checking that a managed object is not deleted in the store
23-
@usableFromInline
24-
func notDeletedObject(for id: NSManagedObjectID) throws -> NSManagedObject {
23+
@inlinable
24+
public func notDeletedObject(for id: NSManagedObjectID) throws -> NSManagedObject {
2525
let object: NSManagedObject = try existingObject(with: id)
2626
guard !object.isDeleted else {
2727
throw CoreDataError.fetchedObjectIsFlaggedAsDeleted

0 commit comments

Comments
 (0)