Juan Kou
Juan Kou

Reputation: 165

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

Upvotes: 1

Views: 4035

Answers (2)

Arjun Othayoth
Arjun Othayoth

Reputation: 451

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

    }

Upvotes: 0

ricardopereira
ricardopereira

Reputation: 11683

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

Upvotes: 4

Related Questions