I am playing around with swift and realm in an IOS app.
I try to reload tableView by using realm.addNotificationBlock. But I don't know how to implement this. Can someone help me with exact code example?
Thanks
You can check the class reference to implement the notification handler that catch the changes in the RLMRealm: http://realm.io/docs/cocoa/0.80.0/api/Classes/RLMRealm.html
In this issue you have a test case (non main thread) using the addNotificationBlock.
I hope this may help you.
UPDATE
Check also the examples: RealmTableViewExample
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
// Set realm notification block
__weak typeof(self) weakSelf = self;
self.notification = [RLMRealm.defaultRealm addNotificationBlock:^(NSString *note, RLMRealm *realm) {
[weakSelf reloadData];
}];
[self reloadData];
}
- (void)reloadData
{
self.array = [[DemoObject allObjects] arraySortedByProperty:@"date" ascending:YES];
[self.tableView reloadData];
}
If you are using addNotificationBlock, The naming of addNotificationBlock: does not seem to be very consistent with the latest Swift naming conventions So you please use this code
notificationToken = realm.observe { (notification, realm) in
}
来源:https://stackoverflow.com/questions/25344678/how-to-use-realm-addnotificationblock