0
0
iOS Swiftmobile~20 mins

CRUD operations with SwiftData in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SwiftData CRUD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you save a new SwiftData entity?

You create a new entity instance and call modelContext.insert(newEntity) followed by try? modelContext.save(). What is the expected behavior?

iOS Swift
@Model
class Task {
  var title: String
  init(title: String) {
    self.title = title
  }
}

let newTask = Task(title: "Buy milk")
modelContext.insert(newTask)
try? modelContext.save()
AThe new task is only saved in memory and lost when the app restarts.
BThe new task is saved persistently and will appear in future fetches.
CThe code throws a runtime error because insert is not allowed before save.
DThe new task is saved but only if you call a special commit method afterwards.
Attempts:
2 left
💡 Hint

Think about what modelContext.save() does in SwiftData.

📝 Syntax
intermediate
2:00remaining
Which code correctly updates a SwiftData entity's attribute?

You have a fetched Task entity and want to update its title. Which option is correct?

iOS Swift
func updateTaskTitle(task: Task, newTitle: String) {
  // update code here
  try? modelContext.save()
}
Atask.title = newTitle
Btask.setValue(newTitle, forKey: "title")
CmodelContext.update(task, with: ["title": newTitle])
Dtask.updateTitle(newTitle)
Attempts:
2 left
💡 Hint

SwiftData entities use property wrappers for attributes.

lifecycle
advanced
2:00remaining
What happens if you delete an entity but forget to save the context?

You call modelContext.delete(entity) but do not call modelContext.save(). What is the result?

AThe entity remains in persistent storage until <code>save()</code> is called.
BThe app crashes with an error about unsaved changes.
CThe entity is deleted from memory and persistent storage automatically.
DThe entity is removed immediately from persistent storage.
Attempts:
2 left
💡 Hint

Think about when changes are committed to disk in SwiftData.

navigation
advanced
2:00remaining
How to refresh a SwiftUI list after SwiftData changes?

You display a list of Task entities fetched from SwiftData. After adding a new task, the list does not update automatically. What should you do?

AWrap the list in a <code>NavigationView</code> to enable automatic refresh.
BManually reload the view by calling <code>setNeedsDisplay()</code> on the list.
CUse <code>@Query</code> property wrapper to fetch tasks so the list updates automatically.
DCall <code>modelContext.refreshAll()</code> after saving the new task.
Attempts:
2 left
💡 Hint

SwiftUI and SwiftData integration uses property wrappers for reactive updates.

🔧 Debug
expert
3:00remaining
Why does this SwiftData save fail with a validation error?

Given this entity definition and save code, why does saving fail?

@Model
class User {
  @Attribute(.unique) var email: String
  init(email: String) {
    self.email = email
  }
}

let user1 = User(email: "a@example.com")
let user2 = User(email: "a@example.com")
modelContext.insert(user1)
modelContext.insert(user2)
try modelContext.save()
ABecause <code>modelContext.save()</code> requires a completion handler.
BBecause you must call <code>modelContext.insert()</code> only once per context.
CBecause the email property must be optional to allow duplicates.
DBecause two entities have the same unique email, violating the uniqueness constraint.
Attempts:
2 left
💡 Hint

Look at the attribute modifiers and what they enforce.