martijnwalraven / meteor-ios

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

NSManagedObjectContextObjectsDidChangeNotification don't work with callMethodWithName #101

Closed chrisscholly closed 7 years ago

chrisscholly commented 8 years ago

Hi, Seems that I can't be notified with NSManagedObjectContextObjectsDidChangeNotification when I use callMethodWithName to perform Mongo update. The update is done locally, no problem, but I don't receive the notification. Example:

// server side
updateLoginCreatedAt: ->
    Meteor.users.update {_id: Meteor.userId()}, {$set: {'createdAt': new Date()}}
// iOS side
func someFunc() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(objectsDidChange), name: NSManagedObjectContextObjectsDidChangeNotification, object: Meteor.mainQueueManagedObjectContext)
    Meteor.callMethodWithName("updateLoginCreatedAt", parameters: nil) { result, error in
        print("updateLoginCreatedAt done!")
    }
}
func objectsDidChange(notification: NSNotification) {
    print("Changed!")
}
// Result
updateLoginCreatedAt done!

// Expected result
updateLoginCreatedAt done!
Changed!

Thanks in advance for your help :)

martijnwalraven commented 8 years ago

I think NSManagedObjectContextObjectsDidChangeNotification only works for objects that have been previously fetched in the managed object context.

This has been a while, but you may have more luck observing NSManagedObjectContextDidSaveNotification.

chrisscholly commented 8 years ago

Thank you for your answer!

Have tried observing NSManagedObjectContextDidSaveNotification but this is the same result :(

chrisscholly commented 8 years ago

METDatabaseDidChangeNotification seems to work better.

Currently I use Core Data to fetch data and METDatabaseDidChangeNotification to observe changes. I'm guessing there's no problem to fetch new data from Core Data when a METDatabaseDidChangeNotification is fired. Does it make sense?

martijnwalraven commented 8 years ago

Yep, that makes sense.

chrisscholly commented 8 years ago

Thank you guy! 👍🏻