Easy use of Realm in Swift.

Easy use of Realm in Swift.

Hi everyone,

In this tutorial we will see how to implement a high speed and robust support for persistent data in a mobile application. We use Realm as database support for persistent data.

Realm is a mobile database that runs directly inside smartphones, tablets or wearables.

Features:

  • Mobile-first: Realm is the first database built from the ground up to run directly inside phones, tablets and wearables.
  • Simple: Data is directly exposed as objects and queryable by code, removing the need for ORM’s riddled with performance & maintenance issues. Most of our users pick it up intuitively, getting simple apps up & running in minutes.
  • Modern: Realm supports relationships, generics, vectorization and Swift.
  • Fast: Realm is faster than even raw SQLite on common operations, while maintaining an extremely rich feature set.

Now let’s to see how to implement it in a iOS demo application.

First of all we need to create a new project on our Mac via XCode. This easy step is shown below.

Select Swift as language and single view as project type. Then type a name for the project. Put for example “MyRealmSwift”. Select Swift as language and deselect “Use Core Data”,”Unit Test” and “UI Test”.

Save your project in to your preferred location and remember the location, we will need it later.

Well, now you can close the project and start Terminal. Type this command to confirm if you have the utility called “pod”. With this utility you can add at your project support of external frameworks. Then type:

 pod  --version

Terminal responds with the version number like the image below:

If you don’t have pod installed try to type this command in your terminal prompt:

sudo gem install cocoapods

And wait to finish the installation. Then try the version command again . If you have trouble installing it please have a look on Google where there is a lot of information available.

When you are sure that all goes well, go to your project directory via “cd” command. Typically Xcode projects are stored in ~/User/Documents/ then from your command prompt type

cd documents (and hit return)
cd MyRealmSwift (and hit return)

At this point you are in your project’s working directory. Type “ls” to see your project files. You should see something like this:

Well, at this point we need to start our pod environment. Type:

pod init

And wait for pod initialization to complete. Don’t close the terminal window. At this point we are ready to edit the Pod file to add our framework. The Pod file is a specification that describes the dependencies of the targets of one or more Xcode projects. The file should simply be named “Podfile”.

Open this file with your preferred text editor and you will see something like this:

platform :ios, '10.3'
    use_frameworks!
    target ‘MyRealmSwift’ do
      *** Insert your pods here ***
    end

On your files insert “pod ‘RealmSwift’” between “target” and “end”:

pod 'RealmSwift'

Then save and exit. From the Terminal now type this command:

pod install

And wait for it to complete. It can take some time depending on our connection speed. You should see an output like this:

Now via your Mac Finder go to your project directory, and now you can find many new files and directories. Be sure to open the new generated XCode project file with .xcworkspace extension. You can easily find it because it has a blank icon instead of a blue icon like normal XCode projects.

Double click on it and XCode will open the project for you. If you have followed all the instructions you will see on the left the XCode project files and the pods files that include the Realm frameworks.

Now we can start to design our App. Open “Main.storyboard” from the left and we can see a blank screen.

Put on it a new tableView component from the right and set it as in the image:

Now it’s time to set the constraints of the tableView. It should be set in fullscreen.

Now we need to embed our view in Navigation View to have a better UI. Go to “Editor” in the top menu and select “Embed In” and select “Navigation Controller”.

Now we need to add some buttons. Add new Bar Button Item and select “Add” as System Item.

Lastly add another View Controller to present our data for insertion, modification and deletion.

Design this new View Controller as in the image below and add a label on the top, a Text Field under the label, a button to delete, and one to save our items. Put it on the bottom or under the Text Field. Add new constraints to those components as you like and continue.

Now it’s time to associate the new components with the code.

First of all add a new Swift Class and name it secondViewController. Set language as ‘Swift’ and set subclass as UIViewController.

Add this class to the second View Controller easily via XCode as shown. Select the second view and in the panel on the right and set the new class.


Now it’s time to associate our view’s components with our code. To do this, click on the icon with two circles on the top-right of XCode. XCode will open two new windows: on the left you can view components and on the right our code. Right click on the mouse or track pad from the component on the left and grag it to the right. Type a name for the components and you should see something like this:

Do the same thing with the buttons, but select “Action” instead of “Outlet” and give it a name. This is a function that will be executed when users tap on the button. Set, for example, saveButtonClicked for the save button and deleteButtonClicked for the delete button.

Click on the button on the left of the double-circle in xcode and click on the first view. Do the same thing as the other view controller. Add an outlet for the tableView and an action for the “+” button.

If all goes well click the play button to start the iOS Simulator with your empty App. Don’t forget to set a name for the secondViewController and set it to “secondView” in the “Storyboard ID”:

We will need it later. Finally we need to set a cell identifier to our Table View. Click on the first view controller and double click on the table view. Select a prototype cell and insert an identifier string as below:

Try your App with the Play button on top of xcode.

We are ready to code now.

First of all we need to add support for the database. We need a class that permits access to our data, no matter how database is used, we need a general class to do it. It’s important to do as many abstractions we can to generalize our code. For example we use Realm, but if we should use MySql or another Database, we need to edit this class with respect to the parameters and the returning values.

Now create a new empty Swift file called DBManager.swift and add this code:

import UIKit
import RealmSwift
class DBManager {
    private var   database:Realm
    static let   sharedInstance = DBManager()
    private init() {
       database = try! Realm()
    }
    func getDataFromDB() ->   Results<Item> {
      let results: Results<Route> =   database.objects(Item.self)
      return results
     }
     func addData(object: Item)   {
          try! database.write {
             database.add(object, update: true)
             print("Added new object")
          }
     }
      func deleteAllFromDatabase()  {
           try!   database.write {
               database.deleteAll()
           }
      }
      func deleteFromDb(object: Item)   {
          try!   database.write {
             database.delete(object)
          }
      }
}

This class is made up of many parts. First of all is a singleton class (I won’t explain what a singleton class is, but you should look up what is it), this is to manage better the operations on the database. There are 4 methods that you can use to read from the database (getDataFromDB), write to database (addData), delete an Item (deleteFromDb) and empty the entire database (deleteAllFromDatabase). Try to run the application and all will go well.

Now it’s time to implement the database on our App. Open the code for the main viewController and find the viewDidLoad methods. Add this code to associate the tableView’s Delegate and Datasource with our class:

override func viewDidLoad() {
       super.viewDidLoad()

       myTableView.delegate = self //Set the delegate
       myTableView.dataSource = self //Set the datasource
}

Now, add this code to refresh our TableView when the view appear.

override func viewDidAppear(_ animated: Bool) {
      
        myTableView.reloadData()
}

Now go over the last parenthesis at the end of the file. Type this code:

extension ViewController: UITableViewDelegate, UITableViewDataSource {

}

I prefer to manage the delegate outside the class to having better management of the code. Well, from here we can start to write our Table View delegate methods. Inside the extension write:

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
       return DBManager.sharedInstance.getDataFromDB().count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
       let cell =      myTableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
       let index = UInt(indexPath.row)
       let item = DBManager.sharedInstance.getDataFromDB() [index] as Item // [4]
       cell.textLabel.text = item.textString // [5]
       return cell
}
func tableView (_tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     if(indexPath.row > -1) {
      let vc = self.storyboard?.instantiateViewController(withIdentifier: "secondView") as! secondViewController
      let index = UInt(indexPath.row)
      let item = DBManager.sharedInstance.getDataFromDB() [index] as Item
      vc.CurrentItem = item
      self.present(vc, animated: false, completion: nil)
    }
}

Now go to the “addButtonClicked” method and add this code:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "secondView") as! secondViewController
vc.currentItem = nil
self.present(vc, animated: false, completion: nil)

This will be executed when the user taps on the “+” button. We set currentItem to nil because we need to create a new Item instance on the secondViewController.

Here, we have to set the data for the Table View. I won’t spent a lot of time to saying what these functions do, but in general the first sets how many rows are present in our Table View, the second creates a new row and prints the data inside the cell. The last is executed when the user taps on a row to select an item. It creates the secondViewController, passing the selected data as variables and shows the new view.

Now skip the class declaration “class secondViewController” and write:

var currentItem: Item?

This creates our variable “currentItem” as optional. Optional means that this variable can be set or not set (with a nil value). We use this variable to pass data between views. Now we have logic to insert and delete an item. It’s time to display the item passed to this viewController if it is set. Go to viewDidLoadin the secondViewController class and write this:

override func viewDidLoad() {
     super.viewDidLoad()
     if let item = currentItem {
        txtNote.text = item.textString
     }
}

This shows our string to the textField to permit the modification, only if currentItem variable is set.

We are a good point , but not finished yet. We need the “Item” data model. We use that model to represent and store our data.

Add a new class and call it “Item” in the “MyItem.swift” file.

Import RealmSwift
class Item: Object {
        dynamic var ID = 0
        dynamic var textString = ""
        override static func primaryKey() -> String? {
              returnID”
        }
}

This declares our data model. Don’t forget the dynamic declaration before the “var” keyword. The method primaryKey() said that the variable “ID” is the primary key of the data model. This is important when we need to update our data. At this point we are ready to insert some data. Go to the viewDidLoadmethod in the mainView class and before the last parenthesis write this:

let item = Item()
item.textString = “Hey! It work!”
DBManager.sharedInstance.addData(object: item)

Now run your project and you should see your first item in the list! Wow!

It works, then delete the last group of code. We want to insert our data via the App interface, right?

Now it’s time to add more complexity at our project.

Well, go to the secondViewController and find the saveButtonClicked method. Insert this code:

let item = Item()
if(currentItem == nil) {
// if is a new Item then calculate a new ID, else use the current ID
    item.ID = DBManager.sharedInstance.getDataFromDB().count
}
item.textString = txtNote.text!
DBManager.sharedInstance.addData(object: item)
self.dismiss(animated: true) { }

Instead, in the deleteButtonClicked method insert this code:

if let item = currentItem {
      DBManager.sharedInstance.deleteFromDB(item)
      self.dismiss(animated: true) { }
}

Now try your App and congratulations! You have made your first iOS App with persistent data support.

You can download the sources at this link: www.riccardorizzo.eu/RealmSwift.zip

Thank you.

Riccardo

要查看或添加评论,请登录

社区洞察

其他会员也浏览了