Data Management & Optimization

Apr 4 2025 · Swift 5.10, iOS 17.0, Xcode 15.4

Lesson 03: Data Management Patterns

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll finish updating TheMet app to use multiple data management patterns. You’ll analyze what technologies the app is using, and look to see if any improvements can be made.

Button("Search the Met") {
  modelContext.insert(MetQuery(query: currentMetQuery))
  showQueryField = true
}
func addMetQuery(currentMetQuery: String) {
  modelContext.insert(MetQuery(query: currentMetQuery))
}
private let modelContext: ModelContext

init(modelContext: ModelContext,_ maxIndex: Int = 30) {
  self.modelContext = modelContext
  self.maxIndex = maxIndex
}
@Observable
class TheMetStore {
  var objects: [Object] = []
  private let service = TheMetService()
  private let maxIndex: Int
  private let modelContext: ModelContext
// ... more code below ...
@State private var store: TheMetStore
init(modelContext: ModelContext) {
  let store = TheMetStore(modelContext: modelContext)
  _store = State(initialValue: store)
}
#Preview {
  let config = ModelConfiguration(isStoredInMemoryOnly: true)
      let container = try! ModelContainer(for: MetQuery.self, configurations: config)
  return MetView(modelContext: container.mainContext)
}
init() {
  do {
    container = try ModelContainer(for: MetQuery.self)
  } catch {
    fatalError("Failed to create ModelContainer.")
  }
}
let container: ModelContainer

var body: some Scene {
  WindowGroup {
    MetView(modelContext: container.mainContext).modelContainer(container)
  }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Understanding Data Management Patterns Next: Conclusion