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

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


(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.


The horse has been beaten to death already, but I couldn't help but reference the Realm code which throws an exception if a Realm Object is created or updated without having a primary key.

+ (instancetype)createOrUpdateInRealm:(RLMRealm *)realm withValue:(id)value {    // verify primary key    RLMObjectSchema *schema = [self sharedSchema];    if (!schema.primaryKeyProperty) {        NSString *reason = [NSString stringWithFormat:@"'%@' does not have a primary key and can not be updated", schema.className];        @throw [NSException exceptionWithName:@"RLMExecption" reason:reason userInfo:nil];    }    return (RLMObject *)RLMCreateObjectInRealmWithValue(realm, [self className], value, true);}