Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Beginning Realm on iOS Part 1: Getting Started

This video tutorial covers modeling a basic data model class with Realm.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3479-beginning-realm-on-ios/lessons/2

Great tutorial. I’m just starting to use Realm in a project, so this video tutorial series has come just in time. I was wondering why your primaryKey class method returns an optional String? since it will always have a value. Shouldn’t it be non-optional?

Hi fordee, it returns String? because you may choose not to have a primary key for your object. By default (e.g. if you do not override this method) it returns nil.

Thanks. Yes, I can see in the documentation they say it returns an optional. However, when I implement it in my code, I return a non-optional String and the compiler doesn’t complain. Strange. I guess it can return a non-optional as well.

Yes, but this is a Swift implementation detail not related to Realm - you can use non-Optional values when an Optional is expected.

Thanks for the great tutorial subject! Have been waiting for it.

Question: If i were to use Int for “id”, does the primaryKey also return “id”?

Yes, the primaryKey() method returns the name of the property; you can see an example here: https://realm.io/docs/swift/latest/#primary-keys

Hello and compliments for the tutorial, I’m enjoying every episode of it!
Now that RealmSwift has reached 1.0, I took the opportunity to start fiddling with it again.

As far as I can tell, it looks like storing native Swift Array or NSArray is not supported. So, for example, if you have the following:

class Person: Object { dynamic var friends: [String]? }

you must find a way to decompose / serialize that array in order to store it in Realm, right? Otherwise it would crash at runtime saying that NSArray is not a supported type.

Since in Foundation we have NSJSONSerializer for free and since NSData is a type supported in Realm, I came up with a little workaround and I’ll be happy to hear your opinion about it. Basically I’m converting the array to NSData everytime it gets updated

[sorry for the bad code formatting]

`
class Person: Object {
dynamic var value = NSData()

dynamic var friends: [String]! {
    didSet {
        try! self.value = NSJSONSerialization.dataWithJSONObject(oldValue, options: .PrettyPrinted)
    }
}

override static func ignoredProperties() -> [String] {
    return ["friends"]
}

}
`

Could it be a good practice? Do you think there is a significant overhead with all of this serialize/unserialize thing?

Thank you and, again, many compliments to you and all of the RW guys! :slight_smile:

Thanks! An effective way to store arrays is to wrap the type you want to have an array of. E.g. if you’d like to have an array of strings in your model you could wrap String in a Realm type - there’s a complete example with code here: http://stackoverflow.com/a/31730894/208205

Thank you! :slight_smile:

realm/util/features.h’ file not found is coming when i compile Project. how i can solve it please ?

I haven’t seen this error so my best guess is to reinstall the Realm pod

I like Realm and I like your tutorial. But the cocoapod gives me trouble because I’m on a MacOS Beta. I had a lot of trouble removing all references of “pods”.

We use CocoaPods because it’s usually easier for most of the readers to get working on the project faster :slight_smile:

Excellent tutorial. You would have an updated version of your project for this version of Swift 3.