Search code examples
iosswiftrealm

How to use realm.addNotificationBlock?


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


Solution

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