martijnwalraven / meteor-ios

Meteor iOS integrates native iOS apps with the Meteor platform (http://www.meteor.com) through DDP
MIT License
742 stars 82 forks source link

METDatabaseDidChangeNotification : Swift documentation #71

Open karlml opened 8 years ago

karlml commented 8 years ago

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 commented 8 years ago

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)
jaddoescad commented 7 years ago

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

jaddoescad commented 7 years ago

Also, how do you parse the results?