我应该为Realm中的每个实体定义主键吗? [英] Should I define the primary key for each entity in Realm?

查看:184
本文介绍了我应该为Realm中的每个实体定义主键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在Realm中设置PK不是必须的,只是可以省略。但在文档中声明:


自动为主键属性创建索引。


我想澄清一些问题:



1)PK的默认值是由Realm ,如果我不自己分配它。是哈希还是什么? (如果我不设置PK并调用 [MyRealmObject primaryKey] ,它返回 nil



2)如果隐式PK是默认索引的?我应该担心它,因为如果它没有索引,是否意味着它影响这个实体的一般性能(例如,获取对象)?



3)每次为每个 RLMObject 子类定义PK是一个很好的做法,对于Realm,只需依赖它由Realm本身定义的内部实现?

解决方案

。)



是的!在Realm中设置主键不是强制性的,也不是必须的,这是为什么它完全取决于开发人员和应用程序的需求来确定它们的实现是否必要。



回答您的问题:



1)没有默认值;您指定您自己的属性之一作为主键。 primaryKey 默认情况下返回nil,因为您需要自己重写,以便向Realm指示要用作主键的属性。有些用户将整数设置为主键,但是通常使用UUID字符串是最常见的。



2)没有隐式主键。您必须使用 [RLMObject primaryKey] 方法显式声明哪个属性是主键,并且THEN它将被索引。 :)



3)在我自己的(业余时间)开发经验中,我通常发现有一个主键使得识别和处理特定对象更容易。例如,如果您跨线程传递对象,则可以简单地传递主键值,并使用 [RLMObject objectForPrimaryKey:] 来重新获取对象。显然这取决于你自己的实现要求。你可能不应该添加一个主键,除非你确定你真的需要一个。



例如,这里是你将添加到你的RLMObject子类,如果你想设置UUID字符串作为主键:

  @interface MyObject:RLMObject 

@property NSString * id

@end

@implementation MyObject

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

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

@end

我希望帮助! / p>

附录要详细说明下面的一些注释,主键对于任何Realm API显然是必需的,具有相同键的对象已经存在于数据库中。例如 + [RLMObject createOrUpdateInRealm:] 将向数据库添加一个新的Realm对象,如果具有该主键的对象不存在,只需更新现有对象除此以外。



因此,在主键是后续逻辑的关键组件的情况下,它们是必需的。但是,由于这些API是可以在Realm中添加/更新数据的不同方式的子集,如果您选择不使用它们,您仍然不需要有主键。


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?

解决方案

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

这篇关于我应该为Realm中的每个实体定义主键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆