ios - Realm: get notified on new insertion / update of object -


i trying out realm.io on swift project. insertion , update of objects pretty straightforward, here comes problem: not able catch new object insertion/update notification.

what want achieve simple, save list of objects in realm. , upon app start/refresh, app request new list of objects remote server , perform realm.add(objects, update:true) (i've set id object's primary key same objects not duplicated), ui end of app should notified whenever there's new object, or existing objects have been updated.

i've tried using realm.addnotificationblock(_:) it's called every time rlmrealmdidchangenotification event, though there no new object/update.

how achieve this?

edit: code sample

public class datastoragemanager { var token : notificationtoken? static let sharedinstance = datastoragemanager ()  public func savelista(list: [a]?, realm:realm) {     self.token = realm.addnotificationblock({ (notification, realm) -> void in         print("database changed")     })     if list?.count > 0 {         try! realm.write {             realm.add(list!, update:true)         }     }   }     } 

you should call addnotificationblock once , not everytime call savelista. move datastoragemanager's init method.

but wrote want update ui whenever list updated, instead of having token inside datastoragemanager class directly add notificationtoken property uiviewcontroller class , call addnotificationblock in view controller's viewdidload method. can directly update ui inside notification block.

edit:

if want update ui when data gets updated cannot use realm's notification system (which sends notification everytime data changed).

you can 1 of following

  1. use kvo on realm objects. described in realm docs
  2. send own nsnotification whenever data updated needs refresh of ui. in case can send nsnotification everytime list gets changed in savelista. register view controller observer notification , update ui whenever receive notification.

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -