Group Group Group Group Group Group Group Group Group Forums

Video Tutorial: Beginning Realm on iOS Part 2: Storing and Retrieving Objects

Learn how to create and persist objects and retrieve them back.

This is a companion discussion topic for the original entry at

Why you dont need to refetch the tasks objects and just do tableView.reloadData ?

Any time you read from a Results object you get the latest data from disc. That’s why to refresh the UI you only need to call tableView.reloadData() and this reads the latest data from the Results object.

1 Like

A great introduction so far to Realm.

One thing I vehemently disagree with is using try! for the realm writes. Even though, as you said, the chances of something going wrong is very small - sometimes stuff goes wrong, and it’s always good practice to handle errors, etc… It doesn’t add much code, and it’s much safer.

For those interested, in place of:

 try! realm.write {

you’d use something like this:

    do {
        try realm.write() {
    } catch let error as NSError {
        print("Realm write error: \(error.localizedDescription)")

You can handle the catch anyway you’d like - I think Realm throws an NSError, so you can grab more info about the error if you cast it as such.

Again, great intro so far to Realm - I just wish all tutorials practiced safer coding.

Good clarification - and one of the key things about Realm I never understood from all the other tutorials I’ve read through on the it!

I agree that exceptions need to be handled properly. However printing to the console isn’t error handling :slight_smile: I avoid adding prints instead of doing something to tutorials so that people are not misled that this is what error handling looks like :slight_smile:

Clearly, which is why I said ‘you can handle the catch anyway you’d like’ :slight_smile:

I wasn’t going to add all the code for a UIAlertController, but if I had, you could use the string from the localizedDescription to populate it.

We’re both saying the same thing - my point is force unwrapping is more dangerous than leaving the skeleton of a catch in place for others to implement.

Looking forward to getting through the rest of this tutorial. I’ve already learned more key elements of Realm in the first two videos than the others I’ve seen!

I believe I can use Realm to store my data on iPhone for offline use and then I can sync it to my iPad later. But do the users need to have iCloud account to get started with my app?

The Realm Mobile Database (what this video course is covering) allows you to store data on your device. If you’d like to add syncing capabilities to your app check out the Realm Mobile Platform: - you can even run the Developer Edition for free on your own server :+1:

Thanks for your reply, I just edited it , but do the users need to have iCloud account to get started to use the app?

I’m not sure about iCloud - after having issues with the Apple review team I gave up using it for good.

That’s what I want to make sure so that I don’t have to deal with iCloud.

I’ve MacOS Sierra 10.12.2 beta (16C41B)
Xcode 8.1 (8B62)

I had to manually setup the project because trouble with cocoapods. Am using Realm 3.01.

I’m not able to go through this part in the code:

@IBAction func createTask(_ sender: AnyObject) {
let realm = try! Realm()
let task = Task(title: taskTitle, priority: taskPrio)

    try realm.write() {

I’m getting the error Arguments labels ‘(title:, priority)’ do not match any avaiable overloads.

Here is a screenshot:

what does your Task class look like?

import Foundation
import RealmSwift

class Task: Object {

dynamic var taskID = NSUUID().uuidString

dynamic var title = ""
dynamic var done = false
dynamic var created = NSDate()
dynamic var priority = 0

override class func primaryKey() -> String? {
    return "taskID"

override class func indexedProperties() -> [String] {
    return ["done"]


1 Like

Tried to use Task() and adding properties and it worked!

@IBAction func createTask(_ sender: AnyObject) {
let realm = try! Realm()
let task = Task() //title: taskTitle, priority: taskPrio)
task.title = taskTitle.text!
//task.priority = taskPrio
try! realm.write() {

navigationController!.popViewController(animated: true)


I have a same error ???

Seems like you forgot super.viewDidLoad()