Kodeco Forums

Introducing Realm: Building Modern Swift Apps with Realm Database

Get started quickly with using Realm in your Swift apps with this free preview chapter from our new book: Realm: Building Modern Swift Apps with Realm Database!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/48-introducing-realm-building-modern-swift-apps-with-realm-database

What is the use/purpose of declaring an enum like this, and then accessing the raw values instead of just accessing variables directly? Furthermore, when primaryKey is called, won’t that return the raw value of the enum element “id” and not the variable declared outside the scope of the enum, the one which has the UUID?

Thanks!

The Property enum contains the names of the fields as strings. Some Realm APIs use strings to introspect and access data on Realm objects - like primaryKey() for example. Realm calls primaryKey() on an object to get the name of the field which is its primary key. So - the method correctly returns the name (instead of a value of some sort). For the book we choose to group the field names in enumerations for purely semantic purpose - to group them and avoid typos.

But if you’re looking for a simpler solution you can always just return the name of the primary key like so:

func primaryKey() -> String? {
  return "id"
}

Hello, I have done this tutorial but I had to install realm swift version 3.13 because I am on xCode 10 and swift 4.2. What I have noticed is that is realm file pretty large, for 4000 inserts is about 370MB, is this normal?
Thanks.

If you’re testing in the Simulator that’s normal; if you want to have a better idea of the actual file size make sure to test on a device with no debugger attached.

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!