如何保持对单个持久化Realm对象的引用

时间:2015-12-16 19:06:30

标签: ios objective-c design-patterns persistence realm

我理解如何使用自动更新结果和域通知来更新我的UI的一般概念。我试图围绕最好的方法来做同样的事情,我的视图控制器只有每个都有一个领域对象(一个例子可能是一个聊天控制器,有一个RLMResults或RLMArray的消息,但只有一个& #34;对话"对象)。

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

方法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

1 个答案:

答案 0 :(得分:1)

方法" A"是正确的,尽管你的对象失效的唯一时间是你已经删除了它,此时通过realmObjectWithID:重新获得它不会产生影响(假设这是一些包装+[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