Real-time data delivery is one of the most important elements of mobile application developing. Primarily for the reason that users want to see up-to-date data in the UI without any additional actions/gestures to update the information. This capability is provided out of the box in Backendless and we call it Real-Time Database. Applications using Backendless SDK for iOS can subscribe to changes happening in a data table in the database. This quick guide will help you to get an understanding how Backendless real-time database works. You will build a basic iOS app displaying a list of movies. The movie list will be dynamically updated any time when the data changes in the database. The changes in the database which will be displayed in the application in real-time include: new movies saved in the database, existing movies modified or deleted.
The sample application developed as a part of this quick start guide requires some data to be stored in the database. This section of the guide contains instructions for populating the database with sample data. There are several ways for getting data into Backendless database, however, in this guide we will consider only two methods, both of them use Backendless Console. The first one imports data from a CSV file, the second one uses REST Console:
Create a file called Movies.csv with the following content:
Open Backendless Console and navigate to Manage -> Import section and import the file as described on the screenshot below:
Click the IMPORT button. This will add the data into the Movies data table. The name of the table in the database is the same as the name of the CSV file.
Open Backendless Console and click the Data icon. Create a new data table with the name “Movies”:
You will be prompted to create columns in the new data table. Cancel out as the columns will be created dynamically with the following instructions. Click the REST Console tab. You will make a single API call which will create data table schema and will save some sample data for the app:
You can verify that the data has been stored in the database by clicking the DATA BROWSER tab:
Create an Xcode project, close Xcode and then install the BackendlessSwift
framework using cocoapods:
Then open the created .xcworkspace file and design the storyboard to look as shown below:
Movies View Controller
is UITableViewController
which represents the Movie
objects. Create the MovieCell
class for displaying movie’s information:
Movies
class to represent Movies objects:MoviesViewController
class:The class starts with the method which initializes Backendless SDK:
There are two functions to render Movies objects in UI:
The following line of code obtains a reference to the Movies
data table:
moviesList
. The moviesList
variable is important for real-time integration, we will come to it a little bit later. And also when data is loaded, it is rendered. In case if an error occurs, we will catch it and also render in the UI. The enableRealTime
function is left empty for now. With the code we have added by now you can already experience the app. To do this build and run the project. You should see the following screen:
Before we continue, let’s review what Backendless Real Time Database is. In most cases, you want your application to display the up-to-date data without any additional user actions (such as pull to refresh for instance) or any additional requests to the server. In Backendless Real Time Database there are three main events, which, once occurred, get your clients app synchronized with the server data:
All our SDKs (JS, Android, iOS and .NET) include necessary logic responsible for establishing and maintaining a connection to our real-time servers. The SDKs also provide the API to subscribe to the events listed above. For more information see the Real-Time Database API documentation.
Now we got to the final part of the guide – adding real-time subscriptions to the code. When a new database record in the Movies
table is created, or an existing one is updated or deleted, we want the app to be notified about it and re-render the UI. Additionally, we need to maintain an up-to-date list in the app. For this purpose the code uses the moviesList
variable. The list of the Movie
objects in the UI will correspond to the objects in moviesList
variable. To add the real-time integration, modify the enableRealTime
function as shown below:
createListener
– new movie record is pushed into moviesList and the UI is re-renderedupdateListener
– the updated movie object, which is the argument of the listener function, replaces one in moviesList and the UI is re-rendereddeleteListener
– the deleted movie object is removed from movieList and the UI is re-renderedLet’s check what we’ve got:
To experience real-time database integration:
That’s all folks, happy coding!