A Parse Server is an application server that simplifies the functionalities needed for the backend of applications for devices and web. It facilitates the work of developers who want to create applications quickly for platforms such as iOS, Android, Unity or Windows, among others.
Let’s get started!
In order to develop our backend we will use the Heroku platform, a platform as a cloud computing service that supports different programming languages.
First let’s create and set up an account in Heroku. For this we access to https://www.heroku.com/.
After that, we open the following GitHub repository, https://github.com/parse-community/parse-server-example, where you can see the different project creation options. Let’s use the option “Getting Started with Heroku + mLab Development” clicking on the purple button to start creating our application.
Creating the application in Heroku
In our case we will call our application: tutorial parse server develapps (must go all together and in lowercase). The APP_ID and the MASTER_KEY they must be unique so we can create random texts through the following website: https://www.random.org/strings/. finally, in the section SERVER_URL, we enter the name of our application where it tells us. We proceed to deploy our app by clicking on “Deploy app”. Once the process is finished, we click on “Manage App” to open it.
In the tab “Settings”, if we select “Reveal Config Vars” we are automatically shown the keys of our application that we will then need to configure our project in Xcode.
Get to work!
We created a project in Xcode from scratch and added the Parse Server library. In the following link we can see the different ways of installing the library: https://github.com/parse-community/Parse-SDK-iOS-OSX.
Once we have the library installed in our project, we open the AppDelegate file.swift, where we import it parse and then use it in the “didFinishLaunchingWithOptions ” method”:
To configure it, we must put the app_id and SERVER_URL variables that we use when configuring Heroku. We have everything ready to start storing and reading data, but previously, let’s see what is on our server. To do this, on the screen of our application in Heroku, application menu, from the tab “Overview”, we access the database mLab MongoDB”. A new window opens with information. The most important is in the tab “Collections”, where we can see different objects that we create for our application, the users that there are, etc.
Let’s create our first object.
In the archive ViewController.swift, we enter the following code into the method viewDidLoad”:
PFObject (className: ) it generates an object of the type that we indicate. Once created, we add attributes like “first” or “age”. In this way, when we use our object “People”you know, we’ll have those properties for whatever object we create. Finally we save our object by an asynchronous call. Note: in order to make the call and not give us problems, you must allow the loading of links without security in the Info.plist:
We launch the project and once the code is executed we go back to the tab “Collections” where we see that the People object has been created. Another way to create it is from the web itself in the tab “Collections”, on the button Add collection”. If we access an object we can see a list of objects of that type that we are creating with JSON format.
A little color
The environment that offers us mLab MongoDB” it is not indicative and difficult to handle since when much more data is available it can not be understood well. To improve it we will create a panel where you can see the data and be able to manage them better.
Accessing https://github.com/parse-community/parse-dashboard we can see how to install, configure and use the panel that will allow us to have our information well organized and structured.
First we install the panel: npm install-g parse-dashboard
After this, we directly execute the panel by command using the following command replacing the data with those of our application:
parse-dashboard — appId yourAppId — masterKey yourMasterKey — serverURL “https://example.com/parse” — appName optionalName
Once this command is executed, if there is no problem, the panel will be available in http://0.0.0.0:4040/. Entering this address from the browser will open our panel. On the left there is a menu where you can access objects (“Browser”), configuration, a console for testing, among other options.
Let’s reap our fruits
To make a query we use PFQuery (className: ), where we put the name of the object we want to order. There are many ways to retrieve the data: by identifier, the first one in the list, an array with all the data, etc.let’s see two of all the possible ways: by identifier and an array.
Using the identifier: we specify the identifier of the object, which is unique and is automatically generated by Heroku, in the function getObjectInBackground(withId: “”).
More specific queries can be made using parameters. Although there are many types, let’s look at three examples:
Something very interesting is to know the number of elements in the array without having to get them all and then count them. Using the following code the server returns the number of elements that are:
Oh no, a rotten fruit!
It is always the case that we introduce a wrong object or that it has to be removed for a certain reason. The process is simple, we retrieve the object either by its identifier or another method and delete it.
Let’s change the harvest
It may happen that at a certain point we want to modify the parameters of an object because they have to be updated. The procedure is as follows: we retrieve the specific object from one of the forms we saw before and once we have it we proceed to modify the necessary parameters and save it again.
Throughout this post we have been able to learn how to create a Parse Server to store our data and use it in an iOS application. This is just the tip of the iceberg and can be deepened much more, for example we can create users for our application, make a registration, a login, upload images, log in in incognito mode and even send us push notifications.
A very useful tool to be able to make small Team projects quickly and easily where you can see the data and interact with them.