How to use realm.addNotificationBlock?

雨燕双飞 提交于 2019-12-06 14:41:13

问题


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


回答1:


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];
}



回答2:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!