RealmContent: Add Real-time Updates to your iOS App in 5 Min

 

Introduction

RealmContent coupled with the Realm Mobile Platform quickly gives the developers the ability to add new content into an iOS App by adding data directly into the Realm Browser app.

RealmContent automatically adds the Realm objects it needs into the app’s own schema and allows the developer to use a number of pre-configured View Controllers to list the available content and to render each of the content “pages”.

The component works mostly automatically while it allows for a lot of customization. It saves developers time and drudgery of implementing the underlying plumbing themselves or even having to roll out a completely new version of the app to change in-app content.

The library is the easiest and fastest way to push content in real-time to your complete user base.

How does it work? The technical crash-course

There are five easy steps to add a dynamic content management system to an iOS app.

1) Import RealmContent

Add RealmContent via CocoaPods or include the source files directly into your project. Then import both RealmSwift and RealmContent in a view controller:

import RealmSwift
import RealmContent

Your app would normally have a list of objects it syncs from the Realm Object Server. Once you import RealmContent it will expose two new models which Realm will add to your default schema: ContentPage and ContentElement.

If you’re using multiple Realm files, add ContentPage and ContentElement to the desired object schema.

2) Create a content list data source

To display a list of the available content in your app you can use the ContentListDataSource class that RealmContent provides you with:

let items = ContentListDataSource(style: .sectionsByTag)

Use .plain for a plain list or .sectionsByTag for a list with sections having the pages split by their tag property.

3) Initialize the data source

Call loadContent(from:) to set which realm file to use as the content source:

items.loadContent(from: try! Realm())

You can also have the data source automatically tell your table or collection view to reload the data whenever changes come in from the Realm Object Server (best option):

items.updating(view: tableView)

4) Implement your table view or collection view data source methods like usual, but fetch data from the content data source. The class offers few methods to do that like: numberOfSections, numberOfItemsIn(section:), titleForSection(section:), and itemAt(indexPath:).

This way you can implement your own table view or collection view data source methods and do any UI setup or other logic that you need. (For more detailed code sample check out the demo app in the RealmContent repo.)

5) Presenting a content “page”

Present a ContentViewController instance to display content. You can do this from a table/collection view tap delegate method, prepareForSegue(_:) method, or from an arbitrary piece of code.

Here’s an example of how would it work after a tap on a table cell:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  tableView.deselectRow(at: indexPath, animated: true)

  let item = items.itemAt(indexPath: indexPath)
  let vc = ContentViewController(page: item)

  navigationController!.pushViewController(vc, animated: true)
}

The presented view controller uses a table view to dynamically show the content from the given ContentPage element. In case you change the content remotely, the changes will be reflected in real-time on screen.

Dynamically add, edit, or remove content

The best aspect of using RealmContent is that you can modify the displayed content at any time - all changes will be synchronized in real-time to all connected users. (And upon connecting for all users who aren’t connected in this very moment of course.)

You can easily use the Realm Browser app to add or remove new pages, add, re-arrange, and otherwise modify blocks of content, and more. You can even change the accent color of your headings and links.

Editing contentn via Realm Browser

Try RealmContent now

The component is open source and you can find the source code and a demo app here: https://github.com/realm-demos/RealmContent.

The quickest way to try RealmContent in your app is via CocoaPods; add pod 'RealmContent' to your Podfile.

If you have an idea for a feature, want to report a bug, or would like to help us with feedback reach us on Twitter or create an issue on GitHub.

We’re excited to see how far are you going to push this component and the awesome dynamic apps you’re going to create with it!

Ready for Realtime and Scale: Announcing Realm Mobile Platform 1.0

We’re proud to announce Realm Mobile Platform has left beta status and graduated to version 1.0! We’ve been hard at work fixing bugs, polishing rough edges, hardening the code, and adding more than a few new features. Learn more in the announcement post!


Marin Todorov

Marin Todorov

Marin Todorov is an independent iOS consultant and publisher. He’s co-author on the book "RxSwift: Reactive programming with Swift" the author of “iOS Animations by Tutorials”. He's part of Realm and raywenderlich.com. Besides crafting code, Marin also enjoys blogging, writing books, teaching, and speaking. He sometimes open sources his code. He walked the way to Santiago.