You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,9 @@ SwiftData introduced modern concurrency features like `@ModelActor`, making it e
27
27
28
28
-**@NSModelActor Macro**
29
29
The `@NSModelActor` macro simplifies Core Data concurrency, mirroring SwiftData’s `@ModelActor` macro. It generates the necessary boilerplate code to manage a Core Data stack within an actor, ensuring safe and efficient access to managed objects.
30
+
31
+
-**NSMainModelActor Macro**
32
+
`NSMainModelActor` will provide the same functionality as `NSModelActor`, but it will be used to declare a class that runs on the main thread.
30
33
31
34
-**Elegant Actor-based Concurrency**
32
35
CoreDataEvolution allows you to create actors with custom executors tied to Core Data contexts, ensuring that all operations within the actor are executed serially on the context’s thread.
@@ -54,6 +57,46 @@ In this example, the `@NSModelActor` macro simplifies the setup, automatically c
54
57
55
58
This approach allows you to safely integrate modern Swift concurrency mechanisms into your existing Core Data stack, enhancing performance and code clarity.
56
59
60
+
You can disable the automatic generation of the constructor by using `disableGenerateInit`:
61
+
62
+
```swift
63
+
@NSModelActor(disableGenerateInit:true)
64
+
publicactorDataHandler {
65
+
let viewName: String
66
+
67
+
funccreateNemItem(_timestamp: Date = .now, showThread: Bool=false) throws-> NSManagedObjectID {
/// Provides access to the NSPersistentContainer associated with the NSMainModelActor.
18
+
varmodelContainer:ModelContainer{get}
19
+
}
20
+
21
+
extensionMainModelActorX{
22
+
/// Exposes the view context for model operations.
23
+
publicvarmodelContext:ModelContext{
24
+
modelContainer.mainContext
25
+
}
26
+
27
+
/// Retrieves a model instance based on its identifier, cast to the specified type.
28
+
///
29
+
/// This method attempts to fetch a model instance from the context using the provided identifier. If the model is not found, it constructs a fetch descriptor with a predicate matching the identifier and attempts to fetch the model. The fetched model is then cast to the specified type.
30
+
///
31
+
/// - Parameters:
32
+
/// - id: The identifier of the model to fetch.
33
+
/// - as: The type to which the fetched model should be cast.
34
+
/// - Returns: The fetched model instance cast to the specified type, or nil if not found.
35
+
public subscript<T>(id:PersistentIdentifier, as:T.Type)->T?where T:PersistentModel{
0 commit comments