领域 RLMObject attributesForProperty

Realm RLMObject attributesForProperty

我刚开始使用领域数据库。我查看了领域文档,发现了一个 RLMObject class 方法 attributesForProperty:

我不明白它在做什么。

你能解释一下我什么时候可以并且需要使用它吗?

感谢您的帮助。

您可以在继承自 RLMObject 的实体 classes 中覆盖此方法,以指定 属性 附加属性,这些属性会影响数据库的架构和行为。目前,您唯一的选择是 属性 是否被编入索引。

假设您有一个模型 class,如文档中所示:

@interface Dog : RLMObject
@property NSInteger age;
@property NSString *name;
@end

使用 v0.91.0 及更高版本:

0.91.0, it is easier to define indexed properties 发布以来。如果你想为 name 列建立索引,那么你可以通过像这里一样覆盖 class 方法来做到这一点。

+ (NSArray *)indexedProperties {
    return @[@"age", @"name"];
}

直到 v0.91.0:

在此版本之前,您可以像下面这样指定索引列:

+ (RLMPropertyAttributes)attributesForProperty:(NSString *)propertyName {
    RLMPropertyAttributes attributes = [super attributesForProperty:propertyName];
    if ([propertyName isEqualToString:@"name"]) {
        attributes |= RLMPropertyAttributeIndexed;
    }
    return attributes;
}