Writing to Database
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 see how we can write data to our database in parse server. So, let's go to our view controller. Because we have already connected our app with our server, so we can actually test to write something to our database, and we're going to use parseObjects in order to achieve this. And as I said before, we're going to use this database for any reason like storage, like authentication, like writing or reading data, so we have to make sure that we know how to do that. So, after importing parse, you can just come over here and create something called parseObject, and this will drive from PF Object class. So, after importing parse, you can call this PFObject. If you open a parenthesis, it will ask you for a class name. So, this class name is actually like a collection name in firebase. So, rather than collections, we have classes over here. So, this is like having a table in SQL. So, we have a user class right now and we have columns. We have some key values over here, in which we can assign new values with new rows. And let me show you what I mean. I'm going to create a fruits class, in which we will have a name of a fruit and a calorie  of a fruit. So, after you say parseObject, you can open a parenthesis and write whatever value that you want here. Rather than value, actually you write a key over here like a name and this name will have something like Apple. So, this is key value pairing like like we did in the firebase like we have specified the key of a field and the value of a field. In here, we have the key of the parseObject as a name and the value of that key as an Apple. So, our object will have the column name and value of Apple under that column. And let me do another example so you can understand it better. I will go for calories and I'm going to say 100. So, I created an object, I gave it a name, I gave it a calories and I gave it some respective values as well. And now, I want to save this. So, if you say parseObject.save, you will see we have a lot of options. So, if you say dot save, it will throw an error, so we have to use try catch and everything, and also this works synchronously. And in most of the cases, we don't want that. We want to work with a synchronous tasks, so that we don't wait for it to happen and we don't block the main user interface so that users can actually continue using the app. So, in most of the cases, we don't want that, we want something within background like the saveInBackground, it works asynchronously. So, we have saveInBackground and we have saveInBackground with block and this gives us a completion block and this gives us a callback function within a closure and we get to see the result of our action, so that's what we're going to use. And saveEventually, it means that whenever you have the chance, just do it. So, in saveInBackground, if we lose the internet connection, for example, we will see an error, but in saveEventually, it will just save it eventually when it finds a chance. So, most of the time in any methods in parse, we're going to use InBackground with block because that's the way to go. So, let me choose this and hit enter. It will give you either this is successful or not as a volume, so you can just write something like success in here as a variable name and it will give you an error if there is any on the other side. So, I'm going to name this success, and I'm going to name this error. So, I will check to see if error is not nil and if error is not nil, most of the time, we're going to display an alert message, but right now I'm just going to print the error.localizedDescription, Else it means that it's successful but right now, I'm just going to say something like uploaded, so that we know this gets uploaded. So, let's run this in our simulator and here you go. We have already seen the uploaded in here. So, let's go to our dashboard and let's refresh this page to see if we got the fruits class. Here we go, we have the fruits class. So, if you click on it, you will see we have some columns, we have some default columns here, like objectID, so this is a unique ID, and this is created by parse itself. So, every time we create an object, it gets its own object ID and this is very useful, we're going to use that in our code as well. So, this has a createdAt, an updatedAt, so this automatically actually uploads or creates the date itself when we create an object. So, this is very good. So, we can just use this if we want something chronological in our app. So, in here we have the ACL, and this is kind of the security rules in firebase. For example, it says that the public can read and write it right now and in here we have the name and the calories. So, these are two columns that are created by us. So, here we go, we managed to save our first information in our back4app server. So, we can create a new column from here, we can manually change data from here. We can refresh the data and we can see track, what's going on inside of our database. So, let's go back. As you can see, it's very easy to create objects, give key value pairings to that object, and save them to the server by just writing a few lines of codes. So, it's very user friendly, it's very easy to follow along. Now, let's stop here, and within the next lecture, we are going to see how we can read data from the server as well.

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