Retrieving the Data

Start course
3h 18m

In this course, we're going to build a Foursquare Clone using Parse. Parse provides you with a great tool to work in cloud servers and you will learn the advantages and disadvantages that come with it.

Intended Audience

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 download the data from the parse server, so that we can show them to the user in our Places View Controller. And remember in Places View Controller, we only have one Table View. So, we don't have to get every data from parse server at this point. We don't have to get the image, we don't have to get the latitude, longitude. We just have to get the names, so that we can display the names. And in addition to name, I'm going to get the object idea as well, so when the user taps one of the TableView items, I'm going to pass this objectID information to the details View Controller and in details View Controller, I'm going to get everything related to one single objectID, so that it would be efficient for me. I'm just going to download image for example, for only one item at one time. So, let's do the TableView set up. As you might remember, we're going to have to add this TableViewDelegate and TableViewDataSource to our PlacesVC, and we have to say TableView.Delegate itself and TableView.DataSource itself. So, remember, we have two more functions to call here and set our TableView up. So, let me just write our array, first because we're going to have to use those arrays in our functions as well. So, first of which will be placeNameArray. This will be a string array and again the placeIdArray, this will be a string array as well as you can see this placeIds are stored as strings. So, later on I can come over here to call my TableView functions, and first of them is cellForRowAt indexPath. So, this one and we're going to have to call number of rows in section as well. So, we're gonna return the placeNameArray.count in here. Okay. And we're going to create a new cell, this is going to be a UITableView cell, and we don't need any fences out here because we're going to just display the place names. So, you can just come over here and say placeNameArray[indexPath.row], and we're going to have to return to cell. So, that's good. I'm doing this very fast because we have done this like a million times by now. So, let me do a command B and see if everything is okay. Yeah everything seems to be okay. Now, let's get this data from parse server. I'm going to do that under another function because it will be some kind of long function. Okay? I don't want to write everything under viewDidLoad but don't forget to call getDataFromParse under viewDidLoad. So, let's see if parse is important. So, we're creating the PFQuery and this will ask us for a class name. So, what is our class name? It's Places with uppercase P. So, later on, I'm going to say query.findObjects in background. So, this will give me some objects and errors. Right? So remember, the objects and the errors, and I'm going to check to see if error is not nil. If error is not nil, I'm going to display an alert message but if error is nil, I'm going to do what I have to do. So, let me cut this error message actually, so that I can create my makeAalert function under here as well. So, this will be makeAlert and if we're going to ask for title Input, okay, this will be String and we're going to ask for message Input as well. This will be again as String. So, rather than saying error, I'm going to say title Input rather than saying message, I'm going to say message Input. So, let's call this function over here first. So, if error is not nil, I'm gonna make Alert and I'm going to call this error and the message will be error.localized Description and I will provide a default value if this, if this is empty or something. And in here I'm going to call self.make Alert one more time and I'm going to get this from error localized description and I will provide a default option as well and we have to say self.make Alert under here as well. And else, if everything works out fine, what do we do? We have to get individual objects and get their values. Right? So, we're going to do this under a for loop. So, if you say for object in objects, it may give an error because this object is actually optional. As you might remember, this gives us an optional PFObject array. So, we have to force cast this actually but before we do that, I suggest, we check to see if object is nil or not even though we know that there is not any area at this point, I believe it's safer to check if object is nil. Okay? So, once we do that, we can call for object in objects and force cast this. So, for object in objects and put exclamation point and here you go. Now, I can get every single object out of that object array. So, if I say object, okay, this one PFObject and if I say 'dot', I can get the values given keys at this point. So, I can reach the PFObject methods and functions because I have reached the individual object. For example, if you say object for key, if you give any key here, like name, latitude, longitude, you can get the name, you can get the latitude, you can get the longitude. All you have to do is just give the key and our key in this case is name. So, let me just write it one more time and you will see this will give me an any type, so I'm going to have to cast this as String. And in order to make this work, of course, I'm going to use an 'if let' and I'm going, I'm going to call this 'if let' place Name. And if this works, I'm going to actually check for place Id as well. And if you say object dot, you will see that objectId actually comes within another method. So, you don't have to give the key name in here. You can just write object.objectId to get unique I. D. of that object. So, this will give us the same I. D. that we are seeing in here. So, let's see object.objectId. Let's try to cast it as string as well. And if that's the case, then I'm gonna add this to placeNameArray first, placeNameArray.append, and I'm going to add the place name. Right? And then later on, I'm going to add this to my placeId Array and this will be placeId, so that I will have the place name and the placeId of created places. And then later on, we're gonna have to call self table TableView.reload Data after the for loop. And remember before the for loop, we have to actually make sure that our arrays are cleared. So, we're going to say remove All, KeepingCapacity false, placeIdArray, remove All, Keeping Capacity false. So, we cleared out the arrays in order to prevent duplication of the values. Now, we get the values, we reload the data, now we are doing everything. Let's see. Here we go. We got our data in TableView. So now, we only have one data but, of course later on, we're going to add another places and tests as well. But now we can get this data and show it to the user in placesViewController. Now, all we have to do is just find a way and the user taps on this. We're going to open the detailsViewController and we're going to show the other details of that chosen place to the user getting the parse data from the parse server. Let's do that in the following lecture.


About the Author
Learning Paths

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.

Covered Topics