A SwiftData like `@Model` infrastructure for CoreData.
Instead of wrapping CoreData, use it directly :-)
The key thing ManagedModels provides is a @Model
macro,
that works similar (but not identical) to the SwiftData
@Model
macro.
It generates an
NSManagedObjectModel
straight from the code. I.e. no CoreData modeler / data model file is necessary.
A small sample model:
@Model class Item: NSManagedObject {
var timestamp : Date
var title : String?
}
import SwiftUI
import ManagedModels
@Model class Item: NSManagedObject {
var timestamp : Date
}
struct ContentView: View {
@Environment(\.modelContext) private var viewContext
@FetchRequest(sort: \.timestamp, animation: .default)
private var items: FetchedResults<Item>
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink {
Text("Item at \(item.timestamp!, format: .dateTime)")
} label: {
Text("\(item.timestamp!, format: .dateTime)")
}
}
.onDelete(perform: deleteItems)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
Text("Select an item")
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
try! viewContext.save()
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
try! viewContext.save()
}
}
}
#Preview {
ContentView()
.modelContainer(for: Item.self, inMemory: true)
}
This is not intended as a replacement implementation of SwiftData. I.e. the API is kept similar to SwiftData, but not exactly the same. It doesn't try to hide CoreData, but rather provides utilities to work with CoreData in a similar way to SwiftData.
A full To-Do list application example: ManagedToDos.app.
Blog article describing the thing: @Model
for CoreData.
The macro implementation requires Xcode 15/Swift 5.9 for compilation.
The generated code itself though should backport way back to
iOS 10 / macOS 10.12 though (when NSPersistentContainer
was introduced).
Package URL:
https://github.com/Data-swift/ManagedModels.git
ManagedModels has no other dependencies.
NSManagedObject
(superclasses can't be added by macros),
e.g. @Model class Person: NSManagedObject
.@FetchRequest
property wrapper instead @Query
.@Model
: Issue 10
addItemToGroup
etc): Issue 11
@Query
property wrapper/macro?: Issue 12
fetchRequest()
class function.var title = "No Title"
): Issue 14
Pull requests are very welcome! Even just DocC documentation or more tests would be welcome contributions.
@Model
for CoreData
SwiftData and SwiftUI are trademarks owned by Apple Inc. Software maintained as a part of the this project is not affiliated with Apple Inc.
ManagedModels are brought to you by Helge Heß / ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.