OGeek|极客世界-中国程序员成长平台

标题: ios - 如何保持对单个持久 Realm 对象的引用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:45
标题: ios - 如何保持对单个持久 Realm 对象的引用

我了解如何使用自动更新结果和 Realm 通知来更新我的 UI 的一般概念。对于我的 View Controller 只有每个都有一个 Realm 对象的情况(例如,聊天 Controller 可能具有 RLMResults 或 RLMArray 消息,但只有一个“对话”对象)。

我已经能够提出以下两种方法,但似乎都不对。实现这一点的正确方法是什么?

方法 A:

@interface ViewController ()

@property(nonatomic, assign) NSInteger objectPrimaryKey;
@property(nonatomic, retain) MyRealmObject *realmObject;

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.objectPrimaryKey = 123;
  self.realmObject = [MyDataManager 
                            realmObjectWithID:self.objectPrimaryKey];

  // Set realm notification block
  __weak typeof(self) weakSelf = self;
  self.notification = [[MyDataManager realm] 
                        addNotificationBlock:^(NSString *note, 
                                               RLMRealm *realm) {
    [weakSelf reloadData];
  }];
  [self reloadData];
}

- (void)reloadData {
  if(self.realmObject.isInvalidated) {
    self.realmObject = [MyDataManager 
                            realmObjectWithID:self.objectPrimaryKey];
  }
  // Populate the UI with self.realmObject
}

@end

方法 B:

@interface ViewController ()

@property(nonatomic, assign) NSInteger objectPrimaryKey;
@property(nonatomic, retain) RLMResults *realmObjectResults;
@property(nonatomic, readonly) MyRealmObject *realmObject;

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.objectPrimaryKey = 123;
  self.realmObjectResults = [MyDataManager
                             realmObjectResultsWithID:self.objectPrimaryKey];

  // Set realm notification block
  __weak typeof(self) weakSelf = self;
  self.notification = [[MyDataManager realm]
                       addNotificationBlock:^(NSString *note, 
                                              RLMRealm *realm) {
    [weakSelf reloadData];
  }];
  [self reloadData];
}

- (void)reloadData {
  // Populate the UI with self.realmObject.
  // Don't think we need to check isInvalid here?
}

- (MyRealmObject *)realmObject {
  return self.realmObjectResults.firstObject;
}

@end



Best Answer-推荐答案


方法“A”是正确的,尽管您的对象唯一会失效的情况是您已将其删除,此时通过 realmObjectWithID: 重新获取它不会有任何影响(假设这是 bjc(cs)RLMObject(cm)objectForPrimaryKey:" rel="noreferrer noopener nofollow">+[RLMObject objectForPrimaryKey:] 的一些包装)

@interface ViewController ()

@property(nonatomic, assign) NSInteger objectPrimaryKey;
@property(nonatomic, retain) MyRealmObject *realmObject;

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.objectPrimaryKey = 123;
  self.realmObject = [MyDataManager 
                            realmObjectWithID:self.objectPrimaryKey];

  // Set realm notification block
  __weak typeof(self) weakSelf = self;
  self.notification = [[MyDataManager realm] 
                        addNotificationBlock:^(NSString *note, 
                                               RLMRealm *realm) {
    [weakSelf updateUI];
  }];
  [self updateUI];
}

- (void)updateUI {
  // Populate the UI with self.realmObject
}

@end

关于ios - 如何保持对单个持久 Realm 对象的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34320104/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4