0
0
iOS Swiftmobile~10 mins

Core Data overview in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Core Data context from the persistent container.

iOS Swift
let context = persistentContainer.[1]
Drag options to blanks, or click blank then click option'
AdefaultContext
BviewContext
CbackgroundContext
DmainContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using backgroundContext instead of viewContext for UI updates.
Trying to access a context property that does not exist.
2fill in blank
medium

Complete the code to save changes in the Core Data context.

iOS Swift
do {
  try context.[1]()
} catch {
  print("Save failed")
}
Drag options to blanks, or click blank then click option'
Acommit
Bpersist
Csave
Dstore
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit() which does not exist.
Forgetting to use try with save().
3fill in blank
hard

Fix the error in fetching Core Data objects by completing the fetch request code.

iOS Swift
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: [1])
let results = try context.fetch(fetchRequest)
Drag options to blanks, or click blank then click option'
AUserEntity
BuserEntity
C"userEntity"
D"UserEntity"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the entity name string.
Using incorrect capitalization.
4fill in blank
hard

Fill both blanks to create a new managed object and set its attribute.

iOS Swift
let entity = NSEntityDescription.entity(forEntityName: [1], in: context)!
let newObject = NSManagedObject(entity: entity, insertInto: [2])
newObject.setValue("John", forKey: "name")
Drag options to blanks, or click blank then click option'
A"Person"
Bcontext
CpersistentContainer
DviewContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using persistentContainer instead of context for insertion.
Not quoting the entity name.
5fill in blank
hard

Fill all three blanks to filter fetch results by a condition.

iOS Swift
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: [1])
fetchRequest.predicate = NSPredicate(format: "[2] == %@", [3])
let results = try context.fetch(fetchRequest)
Drag options to blanks, or click blank then click option'
A"Person"
Bage
C30
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using attribute names without quotes in the format string.
Passing a string instead of a number for age.