Mobile App Development for iOS
Notes Topics Weekly Resources Graded work Professor Code examplesStudents in this course are new to Core Data technologies. However, all students have some knowledge of and experience with other data store technologies. This document attempts to compare some syntax forms among technologies.
Assume a “Person” entity for all examples. (The plural form is used with MongoDB via Mongoose and SQL.)
Core Data
let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
do {
let results = try m.ds_context.fetch(fetchRequest)
// Handle the results
} catch let error {
print(error.localizedDescription)
}
MongoDB via Mongoose
let results = db.people.find()
SQL
select * from people
Core Data
let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
// Filter the results
fetchRequest.predicate = NSPredicate(format: "age < 25", argumentArray: nil)
// Sort the results
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
let results = try m.ds_context.fetch(fetchRequest)
// Handle the results
} catch let error {
print(error.localizedDescription)
}
MongoDB via Mongoose
let results = db.people.find(
{ age: { $lt: 25 } }
).sort(
{ name: 1 }
)
SQL
select * from people where age < 25 order by name asc
Core Data
// Assume m is the data model manager
let peter = Person(context: m.ds_context)
peter.name = "Peter"
peter.age = 23
m.ds_save()
MongoDB via Mongoose
var result = db.people.insertOne(
{ name: "Peter", age: 23 }
)
SQL
insert into people values ("Peter", 23)
Core Data
// Assume m is the data model manager
// and "peter" is a reference to an existing fetched object
peter.name = "Peter"
peter.age = 23
m.ds_save()
MongoDB via Mongoose
let result = db.people.updateOne(
{ _id = "5dc1aa2dfc13ae7900000007" },
{
$set: { name: "Peter", age: 23 },
$currentDate: { lastModified: true }
}
)
SQL
update people set name = "Peter", age = 23 where _id = "5dc1aa2dfc13ae7900000007"