Xamarin iOS with Realm Mobile Database

Xamarin iOS with Realm Mobile Database

XamarinRealm

Short introduction

Xamarin is getting more and more popular! It shouldn’t be a surprise especially while talking about creating mobile apps in efficient way.

Most of the mobile apps nowadays need to store data not only to improve performance but also for a better user experience when loading selected screens.

In this article I would like to introduce Realm – mobile database dedicated for iOS, Android and also for Xamarin! Let me show you how easily you can use it withing your Xamarin iOS application.

You can find the source code on my Git Hub.

What do I need to start?

1) Xamarin Studio installed

2) Mac (In my case this is MAC Mini)

3) iOS emulator

Let’s start

1) Open Xamarin Studio and create new empty iOS project:

XamarinRealm1

2) Create new Xamarin iOS app called “XamarinRealm” and select iOS platform only:

XamarinRealm2

XamarinRealm3

XamarinRealm4

3) Now we need to add NuGet package called “Realm” to enable using Realm database in our iOS application:

a. Right click on the project and select “Add NuGet Packages…”:

XamarinRealm5

b. In the search window please type “realm”, select package and click “Add Package”:

XamarinRealm6

c. Rebuild the whole project:

XamarinRealm7

4) Add “Model” folder to your project:

a. Right click on the project and select “Add new folder”:

XamarinRealm8

b. Give it a name – “Model”:

XamarinRealm9

5) Add “Car” class inside Model folder:

a. Right click on the “Model” folder and select “Add new file”:

XamarinRealm10

b. Select C# class and type the name for it – “Car”:

XamarinRealm11

c. “Car” class should derived from “RealmObject” class:

XamarinRealm12

d. Right click on RealmObject to add proper namespace for it:

XamarinRealm13

e. Now add two string properties to the class: “Brand” and “Model”:

public class Car: RealmObject
 {
   public string Brand { get; set;}
   public string Model { get; set;}

   public Car(){}
 }

XamarinRealm14

6) Now it is time to create some simple UI for our app:

a. Add TextField called “CarBrandTextField”

b. Add TextField called “CarModelTextField”

c. Add Button called “AddCarButton”

d. Add Label called “InfoLabel”

We will use them to create new car object, save it in the database and show how many cars are already in it.

XamarinRealm15

XamarinRealm16

e. We also need to add “Touch Up Inside” event to handle button click:

XamarinRealm17

 

7) After creating some UI we need to implement code behind for it:

Once user click “AddCarButton” we would like to create new Car object and save it in the Realm database.

See how easily you can insert objects into Realm mobile database:

a. Get reference to Realm object which handle database operations:

var realm = Realm.GetInstance();

b. Once you initialized the “realm” variable you are able to insert new Car object into database:

realm.Write(() =>
  {
   var newCar = realm.CreateObject<Car>();
   newCar.Brand = CarBrandTextField.Text;
   newCar.Model = CarModelTextField.Text;
  });

As you can see above we are creating new Car object with Brand and Model set in the TextFields and saving it into the database. That’s it! Easy, don’t you think?

c. Once we insert new object we would like to display information how many objects are actually stored in the database. To do it you need only one line of code:

var cars = realm.All<Car>();

 

d. The whole code behind to handle creation of new Car objects looks like below:

public partial class ViewController : UIViewController
  {
   protected ViewController(IntPtr handle) : base(handle)
   {
     // Note: this .ctor should not contain any initialization logic.
   }

   public override void ViewDidLoad()
   {
     base.ViewDidLoad();
   }

   public override void DidReceiveMemoryWarning()
   {
     base.DidReceiveMemoryWarning();
   }
 
   private void addNewCar()
   {
      var realm = Realm.GetInstance();
      realm.Write(() =>
       {
         var newCar = realm.CreateObject<Car>();
         newCar.Brand = CarBrandTextField.Text;
         newCar.Model = CarModelTextField.Text;
       });
      var cars = realm.All<Car>();
      InfoLabel.Text = "Count cars: " + cars.Count();
      CarModelTextField.ResignFirstResponder();
   }

   partial void AddCarButtonClick(UIButton sender)
   {
     addNewCar();
   }
 }

8) Launch the application and try to add some cars to the database!

XamarinRealm18

XamarinRealm19

As you can see it is really easy to use it. There are also some other operations that you can use:

1. You can use LINQ to query:

var cars= realm.All<Car>().Where(d => d.Brand.Equals("BMW"));

2. You can use LINQ syntax:

var cars = from cars in realm.All<Car>() where cars.Brand.Equals("BMW") select cars;

 

Sum up

Using mobile databases was never easier than now. Of course you can use many different alternatives like SQLite but I think that Realm is good choice especially if you want to save time.

You can also find more about Realm mobile database on the official website.

Advertisements