Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I define the primary key for each entity in Realm?

Tags:

I have noticed that setting PK is not obligatory in Realm and simply can be omitted. But in documentation is stated that:

Indexes are created automatically for primary key properties.

And I'd like to clear up some questions:

1) What is the default value for PK is defined by Realm, if I don't assign it by myself. Is it hash or whatever ? (If I don't set PK and call [MyRealmObject primaryKey] it returns nil)

2) If this implicit PK is indexed by default ? Should I worry about it, because if it is not indexed, does it mean that it affects the general performance of this Entity (for example,fetching objects) ?

3) Is it a good practice to define PK every time for each RLMObject subclass or it isn't necessary for Realm and simply may rely on it's internal realization defined by Realm itself?

like image 951
David Avatar asked Sep 01 '15 02:09

David


People also ask

What is primary key in Realm?

The @PrimaryKey annotation will mark a field as a primary key inside Realm. Only one field in a RealmObject class can have this annotation, and the field should uniquely identify the object. Trying to insert an object with an existing primary key will result in an RealmPrimaryKeyConstraintException .

How to set primary key in Realm android?

Enabling them is very easy. Firstly, you add a property to your Realm model class that you would like to use as a primary key. After that, for Cocoa you simply override the 'primaryKey()' class method of the Realm Object to declare to Realm that this is your designated primary key.

What is primary key Kotlin?

Define a primary key Each Room entity must define a primary key that uniquely identifies each row in the corresponding database table. The most straightforward way of doing this is to annotate a single column with @PrimaryKey : Kotlin Java.


1 Answers

(Disclaimer: I work for Realm.)

Yep! Setting a primary key in Realm isn't obligatory, nor necessary, which is why it's completely up to the developer and the requirements of the app to determine whether it's necessary or not in their implementation.

In response to your questions:

1) There are no default values; you specify one of your own properties as a primary key. primaryKey returns nil by default since you need to override that yourself in order to indicate to Realm which property you want to act as a primary key. Some users have set integers as primary keys, but more often than not, using a UUID string is the most common.

2) There's no implicit primary key. You must use the [RLMObject primaryKey] method to explicitly state which property is the primary key, and THEN it will be indexed. :)

3) In my own (spare-time) development experience, I usually find having a primary key makes it a lot easier to identify and handle specific objects. For example, if you're passing an object across threads, you can simply pass the primary key value and use [RLMObject objectForPrimaryKey:] to refetch the object. Obviously this depends on your own implementation requirements. You probably shouldn't add a primary key unless you find out you really need one.

As an example, here's what you would add to your RLMObject subclass if you wanted to set a UUID string as a primary key:

@interface MyObject : RLMObject

@property NSString *uuid;

@end

@implementation MyObject

+ (NSString *)primaryKey
{
   return @"uuid";
}

+ (NSDictionary *)defaultPropertyValues
{
   @{@"uuid": [[NSUUID UUID] UUIDString]};
}

@end

I hope that helped!

Addendum: To elaborate upon some of the comments made below, primary keys are explicitly necessary for any Realm APIs that change their functionality depending on if an object with the same key already exists in the database. For example +[RLMObject createOrUpdateInRealm:] will add a new Realm object to the database if an object with that primary key doesn't already exist, and will simply update the existing object otherwise.

As such, in these instances where a primary key is a critical component of the subsequent logic, they are required. However, since these APIs are a subset of the different ways in which it is possible to add/update data in Realm, if you choose to not use them, you still not required to have a primary key.

like image 75
TiM Avatar answered Oct 25 '22 07:10

TiM