美文网首页
Realm使用createInDefaultRealmWithV

Realm使用createInDefaultRealmWithV

作者: 千若逸 | 来源:发表于2018-11-29 11:20 被阅读26次

刚开始学习使用Realm时,看到有的示例代码使用createInDefaultRealmWithValue创建新的Realm对象,有的使用[realm addObject:]创建新对象,对两者的区别并不十分清楚。直到今天定位一个问题才发现区别大了。

    Person *person = [[Person alloc] init];
    person.personId = @"3456";
    person.sex = @"man";
    person.status = YES;
    
    [realm transactionWithBlock:^{
        //        [realm addObject:person];
        [Person createInDefaultRealmWithValue:person];
    }];
    
    Dog *dog1 = [Dog new];
    dog1.num = 1;
    dog1.name = @"aaa";
    [realm transactionWithBlock:^{
        [realm addObject:dog1];
        [person.pets addObject:dog1];
    }];
    NSLog(@"pets count is %zd", person.pets.count);
    [realm transactionWithBlock:^{
        Person* personGet = [Person objectForPrimaryKey:@"3456"];
        NSLog(@"delay pets count is %zd", personGet.pets.count);
        
    }];

见上面的代码,打印日志结果为:

pets count is 1
delay pets count is 0

看上去[person.pets addObject:dog1]这一句并没有对RLMArray属性数组写入成功,但也没有报错,为什么会有这样的结果呢?

可以看一下 Realm源码注释 解读 <二> 这里面的一段代码注释:

[realm transactionWithBlock:^{
        //存入方式一:aPerson会在存储的时候根据KVC赋值给新建的一个Person模型,并将新建的存入realm.(person指针变了)
        [Person createInDefaultRealmWithValue:aPerson];
        [Person createInDefaultRealmWithValue:bPerson];
        //存入方式二:不用新建(person指针不变)
        [realm addObject:aPerson];
        [realm addObject:bPerson];
    }];

重点在于这一句:存入方式一:aPerson会在存储的时候根据KVC赋值给新建的一个Person模型,并将新建的存入realm.(person指针变了),这说明使用createInDefaultRealmWithValue进行存储时并不 是直接保存value参数,而是在内部新建一个Person模型,通过遍历value参数的属性列表对新的Person模型进行赋值的。也就是说使用这种方式时存储在Realm模型的Person对象与参数Person对象不是同一个对象,指针是不一样的,尽管它们的属性值相同。

这里就要说到一个Realm的修改原则:通过transactionWithBlock对一个模型进行修改时,这个模型一定要是被Realm管理的模型。通过这个例子,是不是对这句话的理解更透彻?

另一段示例代码:

//获取Realm对象
RLMRealm *realm = [RLMRealm defaultRealm];
Student *stu4 = [[Student alloc]initWithValue:@{@"num": @4, @"name":@"titan4"}];
//添加数据
[realm transactionWithBlock:^{
    //添加模型
    [realm addObject:stu4];
    // 这个模型stu, 已经被realm 所管理, 而且, 已经和磁盘上的对象, 进行的地址映射
}];

// 这里修改的模型, 一定是被realm所管理的模型
[realm transactionWithBlock:^{
    stu4.name = @"coder4";
}];

换一种方式来解释第二段代码,那就是,[Person createInDefaultRealmWithValue:aPerson]中的参数aPerson不是被realm管理的模型,这个方法返回的Person对象才是;[realm addObject:aPerson]中的参数aPerson是被realm管理的模型

搞清楚原理了,那么对第一段的问题代码,有3种解决办法:

  1. Person *person前面加上__block,[Person createInDefaultRealmWithValue:person];改成person=[Person createInDefaultRealmWithValue:person];
  2. [person.pets addObject:dog1]的下一行加入[[RLMRealm defaultRealm] addOrUpdateObject:person]
  3. [Person createInDefaultRealmWithValue:person] 替换成 [realm addObject:person]

显然使用[realm addObject:person]是最方便最省事的方法,所以能使用就用[realm addObject:]

相关文章

网友评论

      本文标题:Realm使用createInDefaultRealmWithV

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