The course is part of this learning path
This course explores maps and how we can integrate them into our apps. We'll leverage map functions like finding the user location, showing the user's location on the map, and choosing a location on the map to get its coordinates. And in order to do that, we're going to be focusing on an app called Travel Book. We're going to integrate Core Data in this Travel Book as well so that we can re-practice what we have learned in the previous section.
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi, within this lecture, we're going to get the data from the Core Data database in order to show them to the tableView that we have just created. And in order to do that, I'm going to go ahead and import the 'CoreData' in our ListViewController. And under here, I'm going to create a new function called 'getData' because we're going to be writing a lot of codes in here. So, it would be nice to have a separate function for that. As usual, I'm going to start by creating my appDelegate variable. So, this will be 'appDelegate', okay? This will derive from 'UIApplication.shared.delegate'. And I'm going to first cast this as 'AppDelegate'. And then, I'm going to say, 'let context = appDelegate.persistentContainer.viewContext', okay? And remember, in order to get the data from the CoreData, we have to create a request, which will become an 'NSFetchRequest', okay? If we open parentheses, it will ask us and enter the name in which we will write 'Places'. And remember, we have to do 'NSFetchRequest' results in here as well. So, if I say, 'request.returnsObjectsAsFaults = false', remember. And then, we can come over here and just fetch the request by saying, 'context.fetch'. And I had to do it with a do-try-catch structure, of course. Because, remember why, if we say, 'context.fetch', it throws an error. So, if you say, 'fetch', as you can see, this gives us an NSFetchRequestResult array and it throws an error. So, we have to do two things. We have to say, 'let results' and assign this to be a variable so that we can go into for loop with that results array. And in here, we have to say, 'try', with do-try-catch structure. So, do-try-catch. If there is any error, I'm just going to 'print("error")', okay? After that, what are we going to do? We're going to check to see if actually we have anything in that results array. So, I'm going to say, 'if results.count > zero', it means that we have something in our array. And if that's the case, I'm going to go into the for loop. But right now, results is in any array. So, I'm going to cast this as an 'NSManagedObject' array. Remember, we have to do that, we have to cast this as an NSManagedObject because that's how we get values out of the individual results using Core Data features. We can just say, 'result.value' and it will ask us for a key. And if I say, 'title', for example, it will give me the title value. So remember, we have start this as a string. Now, we're getting this as any and we can just say cast this as a string. And if that's the case, we have our variable back from the CoreData. And the best way to do that with is actually with 'if let'. So, 'if let title = result.value', 'as? String', then it means I have the title. So, let's do that for ID as well. So, 'if let id = results.value(forKey: "id")' as? UUID, rather than ID, then I have my ID as well. And we don't have our ID array and name array. So, let's create those. So, I'm going to call this title array. This will be an array of strings, actually. And an 'idArray', this will be an array of UUID objects, right? This will be initially empty. And in here, we're going to say, 'self.titleArray.append'. And I'm just going to say, 'title'. And in here, 'self.idArray.append(id)'. Once I refresh my titleArray data and idArray data, it means that I have to refresh my tableView as well. But remember, we have to remove everything from these arrays before we go into the for loop in order to prevent duplication of data like we did in the previous section. And after we're done, we can just call 'tableView.reload' data in order to make sure that this refresh data gets reflected in the tableView as well. So, 'tableView.reload' data. And here, we're not going to return 10, we're just going to 'return titleArray.count'. And in here, we're just not going to show 'test' but 'tittleArray(indexPath.row)'. So, this will display the titleArray content in the tableView and we're getting that content in the getData. So, we have to call getData function in the viewDidLoad because we haven't called it yet. So, if we want it to be executed, then we have to call this. And here you go. We have our Eiffel Tower data in the tableView. Let's try to add another one. Let's go for Arc de Triomphe right now. Let me just zoom in a little bit so we can see it better, okay? If you have been to Paris, this is an arch representing the triumph of the French army. So, I'm going to go for 'Arc de Triomphe', okay? And for the comment, I will just say, 'arch', okay? And I believe that's good. So, let me press here for three seconds. And as you can see, now we can see the annotation. And if I hit 'Save', then it shows success. If I go back, I won't see the record because we haven't implemented that feature yet. So, let me run this from the beginning and here you go. We now see the record here. So, it means that we get to display what is on the Core Data database. So, let's stop here. And within the next lecture, let's overcome these problems that we already have in our app.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.