Midhet Sulemani
Posted on November 13, 2019
I am here with another Core Data tutorial, this will be about fetching data from our database, and we will use the same project that we used in the previous tutorial to show a single contact on the screen.
If you haven’t read the previous tutorial, you can check that out here.
This will be a relatively short tutorial, but it is a bit complicated if you’re a beginner, so hang in there…
We will now make a new screen to display a single contact’s information and we will create a segue from our Contacts Table View Controller to the new ViewController. You should add some identifier to the segue, since we will need it. I have named mine “gotoSingleContact”
Now, we will pick another function from the UITableViewDelegate protocol, which is called didSelectRowAt function
In that function we will write a block of code which will fetch our contact through it’s first name.
Note: Since the scope of my project is smaller, I have not used a userId in this case, but if you feel like adding a userId to properly distinguish between two contacts, you could do that.
Now, back to our didSelectRowAt function:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Contact")
if let contactFirstName = allContacts[indexPath.row].firstName {
let predicate = NSPredicate(format: "firstName = %d",
argumentArray: [contactFirstName])
fetchRequest.predicate = predicate
}
do {
if let contacts = try helper.context.fetch(fetchRequest) as? [Contact],
let selectedContact = contacts.first {
performSegue(withIdentifier: "gotoSingleContact", sender: selectedContact)
}
} catch {
print("No contacts found")
}
}
As you can see, we have added a performSegue function call in the didSelectRowAt function. Our purpose here will be that when we select the particular cell, the fetch request will fetch the single contact and pass its value over to the new screen.
So, we will call a prepareForSegue function here:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "gotoSingleContact" {
if let nextVC = segue.destination as? DisplaySingleContactViewController,
let selectedContact = sender as? Contact {
nextVC.selectedContact = selectedContact
}
}
}
Note: I have been having some problems with my Xcode lately, so haven’t been able to test this thoroughly. Anyway, if you get stuck somewhere or come across some bug, please comment below. You could also raise any issue in the GitHub project linked below.
Additional tips: Fetching for core data is one of the uses of NSPredicates, but NSPredicate is a very powerful class and can be used to filter and classify for a lot of other purposes in Swift as well. If you would like to know about NSPredicates in CoreData further, please comment below, or you can read about the scope of NSPredicates in Swift in the Apple Documentation.
The completed project for this tutorial is linked here.
Posted on November 13, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.