CoreData -> AppDelegate -> Create Persistent Container (by Default) with ViewContext // Select coredata checked when creating new project
ManagedObjectConext(from AppDelegate) -> Model Class via init( ) method -> EnvironmentObject -> ContentView
Model Class:
properties:
context // ManagedObject Context
students // Data fetched from CoreData
name // textfield property
selection // Used by List to track selected student
saved // Track save action on status bar
methods:
getStudents // Fetch all students if 'name' string is empty, else fetch names containing string in 'name' property
addStudent // Add a new Student Object in CoreData using string in 'name' property
deleteStudent // Delete specific student from list
deleteStudents // Delete all students if 'name' string is empty, else delete names containing string in 'name' property
save // Save the context to CoreData
-
Create A New Xcode Project -> Select MacOS Tab -> Select App
-
Give Product Name -> Select Language as Swift -> Select User Interface as SwiftUI -> Check Core Data option
-
Select Product_Name.xcdatamodeld file from Project Navigator on left side -> Create New Entity & Rename it
-
Add Needed Attributes and Relationship in CoreData Model Editor
-
The coredata app template already filled AppDelegate.swift with necessary CoreData codes. But we need to inject Model Object in to content view instead of context. Make the following changes in AppDelegate.swift:
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var model: Model!
var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
model = Model(persistentContainer.viewContext)
let contentView = ContentView().environmentObject(model)
...
}
}
-
Add a new Swift File to Manage the Data (Here it is named Model.swift) -> Create a new sub class in it with
ObservableObject
as superclass. -
Add following initialiser and Published wapped property in the subclass (Here, the subclass is
final class Model
)
final class Model: ObservableObject {
@Published var context: NSManagedObjectContext
init(_ moc: NSManagedObjectContext) {
self.context = moc
}
}
-
Add
@Published
property wrapper will all the publically required properties in the subclass -
Use
@EnvironmentObject var model: Model
to access the CoreData Object in Model class.
- Unable to access model inside sheet
Solution: Inject model in to Modal View (ModalView(showModal: self.$showModal).environmentObject(self.model)
- The autocompletion is not working
Solution: Go to preference -> Location tab -> Access Derived folder location and Delete the DerivedData
folder and restart Xcode
This code help the user to properly closed with red close button on window
// AppDelegate.swift
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
...
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}