Build offline-first mobile apps without pain

Designing for intermittent connections is easier when the platform handles serialization, sync, and conflict resolution

1 2 Page 2
Page 2 of 2

Now, we’re ready to make changes to the viewDidLoad() function of our view controller. We’ll also add the setupRealm() function, which will handle signing into a synced Realm. And we’ll add notifications so that whenever and wherever data changes in that Realm, the tableView here will be updated:

// Add this property to the ViewController:
var notificationToken: NotificationToken!
override func viewDidLoad() {
       super.viewDidLoad()
       print(Realm.Configuration.defaultConfiguration.fileURL!)
       setupRealm()
       navigationItem.setRightBarButton(
       UIBarButtonItem.init(
       barButtonSystemItem: UIBarButtonSystemItem.add,
       target: self,
       action: #selector(displayTaskAlert)), animated: false)
   }

       func setupRealm() {
       let username ="YOUR_ADMIN_EMAIL"
       let password ="YOUR_ADMIN_PASSWORD"
       SyncUser.logIn(with: .usernamePassword(username: username, password: password, register: false), server: URL(string: “http://127.0.0.1:9080”)!) { user, error in
           guard let user = user else {
               fatalError(String(describing: error))
           }
           DispatchQueue.main.async {
               // Open Realm
               let configuration = Realm.Configuration(
                   syncConfiguration: SyncConfiguration(user: user, realmURL: URL(string: “realm://127.0.0.1:9080/~/tasks”)!)
               )
               self.realm = try! Realm(configuration: configuration)
               // get or create task list
               func updateList() {
                   let lists = self.realm.objects(TaskList.self)
                   if lists.count == 0 {
                       try! self.realm.write {
                           self.realm.add(self.taskList)
                       }
                   }
                   self.taskList=lists[0]
                   self.tableView.reloadData()
               }
               updateList()
               // Notify us whenever data Realm changes
               self.notificationToken = self.realm.addNotificationBlock { _ in
                   updateList()
               }
           }
       }
   }

That’s seriously it. Now, you can open your synced Realm in the Realm Browser and watch as synced data joins the list! Take your phone into airplane mode, append a few items using the Realm Browser interface, and see that they too are there when you come online again. Tasks will even show up on your task list in the order that they were created, regardless of which device added them, even if it that device was offline.

From here, you could add an interface to enable signing up and logging in as a different user. You could use our permissions features to enable sharing of to-do lists. And you could probably add a way to look at multiple lists, or to complete or delete the tasks that are in your to-do list. In fact, your app might end up looking like the RealmTask sample app, which you can check out in several different languages.

There’s a lot you can build in an offline-first world if you can put aside the pain of networking and serialization code, and instead focus on the core features of your app. Offline-first is a commitment that users appreciate, and Realm makes it easy to do it right.

New Tech Forum provides a venue to explore and discuss emerging enterprise technology in unprecedented depth and breadth. The selection is subjective, based on our pick of the technologies we believe to be important and of greatest interest to InfoWorld readers. InfoWorld does not accept marketing collateral for publication and reserves the right to edit all contributed content. Send all inquiries to newtechforum@infoworld.com.

Copyright © 2017 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2