美文网首页Realm
Realm存储的类型和Realm数据库关系存储制

Realm存储的类型和Realm数据库关系存储制

作者: 爱做梦的星星 | 来源:发表于2017-07-08 21:56 被阅读110次

    文档类型 : 学习笔记


    Realm文章链接:

    1.iOS Realm简单使用(增删改查和排序)

    2.Realm存储的类型和Realm数据库关系存储制

    3.Realm可空属性,默认值,忽略属性,通知

    4.Realm用户机制,数据库操作,数据库迁移


    1.Realm支持的类型

    BOOL, bool, int, NSInteger, long, long long, float, double, NSString, NSData, NSDate, NSNumber
    
    //支持以上几种格式.
    
    //关于集合类或者对象类可以转化成NSData类存储,这边举例图片
    //存储对象类
    @interface student : RLMObject
    
    @property NSString *name;
    @property int number;
    @property UIImage *image;
    
    @end
    
    // This protocol enables typed collections. i.e.:
    // RLMArray<student *><student>
    RLM_ARRAY_TYPE(student)
    
    //使用代码
    student *stu = [[student alloc] init ];
    stu.name = @"xxx";
    stu.number = 25;
    stu.image = [UIImage imageNamed:@"xxx"];
    //这样写是直接报错的
    //不能直接存UIImage
    
    //方法: 
    //增加一个属性
    @property NSData *imageData;
    //忽略image属性字段,让他存NSData属性字段
    //1.在image属性字段前面加上readonly,Realm内部有操作这种readonly的字段会直接忽略
    @property (readonly)UIImage *image;
    //2.在.m中的忽略方法中返回
    + (NSArray<NSString *> *)ignoredProperties
    
    //然后重写image属性字段set方法,这样就OK了
    - (void)setImage:(UIImage *)image {
        _image = [UIImage imageWithData:_imageData];
    }
    // 字典和数组也可以这么干,序列化存储
    
    

    Realm数据库关系存储

    1.1对1

    // 学生类
    #import <Realm/Realm.h>
    #import "Dog.h"
    
    @interface student : RLMObject
    
    @property NSString *name;
    @property int number;
    @property Dog *pet;
    
    @end
    
    // This protocol enables typed collections. i.e.:
    // RLMArray<student *><student>
    RLM_ARRAY_TYPE(student)
    
    // 小狗
    #import <Realm/Realm.h>
    
    @interface Dog : RLMObject
    @property NSString *name;
    @end
    
    // This protocol enables typed collections. i.e.:
    // RLMArray<Dog *><Dog>
    RLM_ARRAY_TYPE(Dog)
    
    // 存储
    Dog *dog = [[Dog alloc] initWithValue:@{@"name":@"anni"}];
    student *stu = [[student alloc] initWithValue:@[@"xxx",@25,dog]];
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        [realm addObject:stu];
    }];
    

    结果:OK,Realm已经帮我们做了关联


    image_1bkh6ktjo1cd06ct82k1op3uir9.png-31.9kBimage_1bkh6ktjo1cd06ct82k1op3uir9.png-31.9kB

    2.1对多

    // 学生类
    #import <Realm/Realm.h>
    #import "Dog.h"
    
    @interface student : RLMObject
    
    @property NSString *name;
    @property int number;
    
    //在realm里面不能用NSArray,用RLMArray,这个里面放的是RLMObject
    @property RLMArray<Dog *><Dog> *pet;
    
    @end
    
    // This protocol enables typed collections. i.e.:
    // RLMArray<student *><student>
    RLM_ARRAY_TYPE(student)
    
    // 小狗
    #import <Realm/Realm.h>
    
    @interface Dog : RLMObject
    @property NSString *name;
    @end
    
    // This protocol enables typed collections. i.e.:
    // RLMArray<Dog *><Dog>
    RLM_ARRAY_TYPE(Dog)
    
    //存储
    Dog *dog = [[Dog alloc] initWithValue:@{@"name":@"anni"}];
    Dog *dog2 = [[Dog alloc] initWithValue:@{@"name":@"xixi"}];
    student *stu = [[student alloc] initWithValue:@[@"xxx",@25,@[dog,dog2]]];
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        [realm addObject:stu];
    }];
    

    结果:OK


    image_1bkh6u3q0ajj1spsff21r3k1aabm.png-29.7kBimage_1bkh6u3q0ajj1spsff21r3k1aabm.png-29.7kB

    3.多对1

    // 狗同时有几个学生主人
    // 狗类.h
    #import <Realm/Realm.h>
    
    @interface Dog : RLMObject
    @property NSString *name;
    
    //用readonly忽略
    @property (readonly) RLMLinkingObjects *master;
    @end
    
    // This protocol enables typed collections. i.e.:
    // RLMArray<Dog *><Dog>
    RLM_ARRAY_TYPE(Dog)
    
    //狗类.m需要方法描述master是啥
    
    
    //学生类
    @interface student : RLMObject
    
    @property NSString *name;
    @property int number;
    
    //在realm里面不能用NSArray,用RLMArray,这个里面放的是RLMObject
    @property RLMArray<Dog *><Dog> *pet;
    
    @end
    
    // This protocol enables typed collections. i.e.:
    // RLMArray<student *><student>
    RLM_ARRAY_TYPE(student)
    
    //存储
    Dog *dog = [[Dog alloc] initWithValue:@{@"name":@"anni"}];
    Dog *dog2 = [[Dog alloc] initWithValue:@{@"name":@"xixi"}];
    student *stu = [[student alloc] initWithValue:@[@"xxx",@25,@[dog,dog2]]];
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        [realm addObject:stu];
    }];
    
    

    结果:OK


    image_1bkh6u3q0ajj1spsff21r3k1aabm.png-29.7kBimage_1bkh6u3q0ajj1spsff21r3k1aabm.png-29.7kB

    这里就有人说了和刚刚1对多不是一样吗?

    这里数据库是看不到的,但是可以直接拿.

    student *stu  = [student allObjects].firstObject;
    NSLog(@"stu的狗的主人是-%@",stu.pet.firstObject.master);
    
    Dog *dog = [Dog allObjects].firstObject;
    NSLog(@"狗的主人是-%@",dog.master);
    

    打印结果:


    image_1bkh7lfbjmbl117ac98cu143k9.png-101.6kBimage_1bkh7lfbjmbl117ac98cu143k9.png-101.6kB

    相关文章

      网友评论

        本文标题:Realm存储的类型和Realm数据库关系存储制

        本文链接:https://www.haomeiwen.com/subject/rcbjhxtx.html