meteor-ios icon indicating copy to clipboard operation
meteor-ios copied to clipboard

METDatabaseDidChangeNotification : Swift documentation

Open karlml opened this issue 9 years ago • 3 comments

We are trying to implement the Meteor-iOS framework in Swift and are attempting to translate from Objective-C. How can we get notified when changes occur to the collection? For some reason databaseDidChange does not fire when the collection is changed.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "databaseDidChange:", name: METDatabaseDidChangeNotification, object: Meteor)

    Meteor.addSubscriptionWithName("snippets") { (error) -> Void in
        if (error == nil) {
            print("success I have subscribet to snippets collection")
        }else{
            print("error: \(error)")
        }
    }

    let snippets = Meteor.database.collectionWithName("snippets")
    let allSnippets = snippets.allDocuments
    print(allSnippets)

}


func databaseDidChange(notification:NSNotification){
    dispatch_async(dispatch_get_main_queue()) {
        print("database did change")
      }
}

Also would it be possible to get a working Swift example for this method, it is so central to the functioning of the framework:

METDatabaseChanges *databaseChanges = notification.userInfo[METDatabaseChangesKey];
[databaseChanges enumerateDocumentChangeDetailsUsingBlock:^(METDocumentChangeDetails *documentChangeDetails, BOOL *stop) {
  ...
}

karlml avatar Oct 27 '15 16:10 karlml

I figured out the first part myself. The NSNotification observer object has to change from Meteor to Meteor.database

NSNotificationCenter.defaultCenter().addObserver(self, selector: "databaseDidChange:", name: METDatabaseDidChangeNotification, object: Meteor.database)

karlml avatar Oct 27 '15 20:10 karlml

have you been able to figure out the second part? for the change details?

jaddoescad avatar Jan 06 '17 02:01 jaddoescad

Also, how do you parse the results?

jaddoescad avatar Jan 06 '17 04:01 jaddoescad