Realm Objective‑C 3.0.1 学习笔记(一)基础知识

 官方文档: realm

疑问:

1. 双向关系 (反向关系)RLMLinkingObjects
2. RLMArray 只能够包含 RLMObject 类型,诸如 NSString 之类的基础类型是无法包含在内的。

1. Realm一般储存位置

根据Data Storage Guidelines 的说明

/Documents  一般是用来保存用户产生的文本和文件,一般不要重复生成,会自动备份到iCloud

/Library/Caches 一般是下载的数据或者可以重复生成的数据放在Caches里,比如数据库缓存文件, 以及电子文档展示时需要的文档

/Library/preference 

/tmp

2.Realm数据类型

Realm 支持下述属性类型:BOOLboolintNSIntegerlonglong longfloatdoubleNSStringNSDateNSData 以及 被特殊类型标记的 NSNumber 。

CGFloat 属性被取消了,因为它不具备平台独立性。

使用 NSNumber * 属性来存储可空数字。必须由 RLMIntRLMFloatRLMDouble 或者 RLMBool 所标记。

@property NSNumber<RLMInt> *age;
  • 必需属性 

通常情况下,NSString *NSData * 以及 NSDate * 属性可以设置为 nil

@interface Person : RLMObject
@property NSString *name;
@property NSDate *birthday;
@end

@implementation Person
+ (NSArray *)requiredProperties {
    return @[@"name"];
}
@end
  • 主键

一旦将带有主键的对象添加到 Realm 数据库,那么该对象的主键将无法更改。

@interface Person : RLMObject
@property NSInteger id;
@property NSString *name;
@end

@implementation Person
+ (NSString *)primaryKey {
    return @"id";
}
@end
  • 索引属性

  Realm 支持为字符串、整型、布尔值以及 NSDate 属性建立索引。

@interface Book : RLMObject
@property float price;
@property NSString *title;
@end

@implementation Book
+ (NSArray *)indexedProperties {
    return @[@"title"];
}
@end
  • 被忽略属性
@interface Person : RLMObject
@property NSInteger tmpID;
@property (readonly) NSString *name; // 只读属性会被自动忽略
@property NSString *firstName;
@property NSString *lastName;
@end

@implementation Person
+ (NSArray *)ignoredProperties {
    return @[@"tmpID"];
}
- (NSString *)name {
    return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}
@end
  • 默认属性值
@interface Book : RLMObject
@property float price;
@property NSString *title;
@end

@implementation Book
+ (NSDictionary *)defaultPropertyValues {
    return @{@"price" : @0, @"title": @""};
}
@end

3. Realm对象

  • 对象的自更新
Dog *myDog = [[Dog alloc] init];
myDog.name = @"Fido";
myDog.age = 1;

[realm transactionWithBlock:^{
    [realm addObject:myDog];
}];

Dog *myPuppy = [[Dog objectsWhere:@"age == 1"] firstObject];
[realm transactionWithBlock:^{
    myPuppy.age = 2;
}];

myDog.age; // => 2
  • 模型继承
@interface Animal : RLMObject
@property NSInteger age;
@end
@implementation Animal
@end

// 与 Animal 一并组合的模型
@interface Duck : RLMObject
@property Animal *animal;
@property NSString *name;
@end
@implementation Duck
@end

@interface Frog : RLMObject
@property Animal *animal;
@property NSDate *dateProp;
@end
@implementation Frog
@end

// Usage
Duck *duck =  [[Duck alloc] initWithValue:@{@"animal" : @{@"age" : @(3)}, @"name" : @"Gustav" }];

4.关系

// Dog.h
@interface Dog : RLMObject
// ... 其余属性声明
@property Person *owner;
@end
  • 多对一关系
// Dog.h
@interface Dog : RLMObject
// ... 其余属性声明
@property Person *owner;
@end

Person *jim = [[Person alloc] init];
Dog    *rex = [[Dog alloc] init];
rex.owner = jim;
  • 多对多关系
// .h
#import <Realm/Realm.h>

@class Person;

// 狗狗的数据模型
@interface Dog : RLMObject
@property NSString *name;
@property Person   *owner;
@end
RLM_ARRAY_TYPE(Dog) // 定义RLMArray<Dog>

// 狗狗主人的数据模型
@interface Person : RLMObject
@property NSString      *name;
@property NSDate        *birthdate;

// 通过RLMArray建立关系
@property RLMArray<Dog> *dogs;

@end
RLM_ARRAY_TYPE(Person) // 定义RLMArray<Person>


// .m
@implementation Dog
@end  // 暂无使用

@implementation Person
@end  // 暂无使用
  • 双向关系 (反向关系)RLMLinkingObjects   ---------
@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@property (readonly) RLMLinkingObjects *owners;
@end

@implementation Dog
+ (NSDictionary *)linkingObjectsProperties {
    return @{
        @"owners": [RLMPropertyDescriptor descriptorWithClass:Person.class propertyName:@"dogs"],
    };
}
@end

 

猜你喜欢

转载自blog.csdn.net/odyyy/article/details/81975020
今日推荐