I understand the general concept of how auto-updating results and realm notifications can be used to update my UI. I am trying to wrap my head around the best approach for doing the same for cases where my view controller only every has a single realm object (An example might be a chat controller that has a RLMResults or RLMArray of messages, but only a single "conversation" object).
I have been able to come up with the two approaches below, but neither seems right. What would be the correct way to implement this?
Approach 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
Approach 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
Aucun commentaire:
Enregistrer un commentaire