- Notifications
You must be signed in to change notification settings - Fork0
Swift Package for managing Core Data concurrency using actors, inspired by@fatbobman’s experimental work, with support for iOS 13+
License
rnine/CoreDataModelActor
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
CoreDataModelActor is a Swift Package that extends and enhances the ideas presented byfatbobman in hisarticle and the associated experimental projectCoreDataEvolution. This package introduces support for older platforms (down to iOS 13) and provides two different executors to manage Core Data concurrency across various versions of Apple's operating systems.
Here’s an example of how to useCoreDataModelActor to manage Core Data operations with actors:
import Foundationimport CoreDataModelActorimport CoreDataModelActorMacros@NSModelActoractorDocumentsRepository{func toggleFavorite(objectID:NSManagedObjectID)asyncthrows{guardlet document=try modelContext.existingObject(with: objectID)as?Itemelse{return} document.timestamp=.nowtry modelContext.save()}}
Or, if you don't want to use the macro:
import CoreDataimport CoreDataModelActoractorDocumentsRepository:NSModelActor{letmodelContainer:NSPersistentContainerletmodelExecutor:anyCoreDataModelActor.NSModelObjectContextExecutorinit(container:NSPersistentContainer){self.modelExecutor=NSModelObjectContextExecutorFactory.makeExecutor(context: container.newBackgroundContext())self.modelContainer= container}func toggleFavorite(objectID:NSManagedObjectID)asyncthrows{guardlet document=try modelContext.existingObject(with: objectID)as?Itemelse{return} document.timestamp=.nowtry modelContext.save()}}
- macOS 10.15
- iOS 13
- tvOS 13
- watchOS 6
- Mac Catalyst 13
This project is inspired by and based on the experimental work done byfatbobman. You can find the original article and project at the following links:
- Article:Core Data Reform: Achieving Elegant Concurrency Operations Like SwiftData
- Experimental Project:CoreDataEvolution
About
Swift Package for managing Core Data concurrency using actors, inspired by@fatbobman’s experimental work, with support for iOS 13+