RAC循环引用

Jan 9, 2017

发表于(2016-09-27 16:35:49)

最近项目到某个节点,我在解决一些问题的时候,记录一下:

  1. RAC 在引用的时候会忽略一点,如
    @weakify(self); @weakify(cell); [[[cell.clearButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(UIButton *x) { if ([self_weak_.selectedNumArray containsObject:asset]) { [self_weak_.selectedNumArray removeObject:asset]; cell_weak_.numButton.selected = NO; [self_weak_.collectionView reloadData]; }else{ if (self_weak_.addMorePicture) { if (self_weak_.selectedNumArray.count==self_weak_.maxPictureCanAdd) { LxDBAnyVar(self_weak_.selectedNumArray); LxDBAnyVar(self_weak_.maxPictureCanAdd); [SHUIHelper showAlertMessage:[NSString stringWithFormat:@"您最多可添加%ld张哦",self_weak_.maxPictureCanAdd]]; }else{ [self_weak_.selectedNumArray addObject:asset]; cell_weak_.numButton.selected = YES; [cell_weak_.numButton setTitle:[NSString stringWithFormat:@"%ld",(unsigned long)self_weak_.selectedNumArray.count] forState:UIControlStateNormal]; } }else{ if (self_weak_.selectedNumArray.count==6) { [SHUIHelper showAlertMessage:@"选取最多6张"]; }else{ [self_weak_.selectedNumArray addObject:asset]; cell_weak_.numButton.selected = YES; [cell_weak_.numButton setTitle:[NSString stringWithFormat:@"%ld",(unsigned long)self_weak_.selectedNumArray.count] forState:UIControlStateNormal]; } } } cell_weak_.grayLayerView.hidden = !cell_weak_.numButton.selected; }];
    上面这里的cell,自己对自己进行了循环引用,原因就是:这一段如果写在该Cell class里面,那么block里面的cell,其实在自身类里面也叫self,于是也需要进行weak处理。
    这里会出现这样的错误,就是因为写得太快了,然后有时候会想不出来,可以慢慢思考。
  2. Realm 的RLMArray 如果添加某一个元素,但其已经存在数据库中,那么做法是先使用[RLMObject objectForPrimaryKey:@“keyID”]找出该元素添加进去,即可

    Just want to add a comment for future users of Realm. I think TiM means[ objectForPrimaryKey:…]. I literally typed [RLMObject objectForPrimaryKey:…] and got a crash about “RLMObject” is not persisted in Realm database