SwiftData in SwiftUI (Part 1)
<h2>Model Container</h2>
<ul>
<li>An object that manages an app’s schema and model storage configuration.</li>
<li>Model Container is set in <strong>WindowGroup</strong>. We can pass multiple data models over there.</li>
</ul>
<pre>
@main
struct SwiftData_ExampleApp: App {
var body: some Scene {
WindowGroup {
NavigationStack {
UserList()
}
}
.modelContainer(for: [User.self, Company.self])
}
}</pre>
<h1>ModelContext</h1>
<ul>
<li>An object that enables you to fetch, insert, and delete models, and save any changes to disk.</li>
<li>Everything in SwiftData happens with model context.</li>
<li>It is basically like view context in CoreData.</li>
</ul>
<pre>
@Environment(\.modelContext) private var modelContext</pre>
<h1>Query</h1>
<ul>
<li>Use <code>@Query</code> in your SwiftUI views to fetch data.</li>
<li>SwiftData and SwiftUI work together to provide live updates to your views when the underlying data changes, with no need to manually refresh the results.</li>
<li>You can add <strong>predicate</strong> to the query to filter the elements, <strong>sort</strong> the query, set the <strong>order</strong>- whether to sort in forward or reverse order and set <strong>animation</strong> for user interface changes that result from changes to the fetched results.</li>
</ul>
<p><a href="https://medium.com/@priya_talreja/swiftdata-in-swiftui-part-1-18919ce2612"><strong>Click Here</strong></a></p>