{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/3431","repository_url":"https://api.github.com/repos/realm/realm-cocoa","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3431/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3431/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3431/events","html_url":"https://github.com/realm/realm-cocoa/issues/3431","id":148170872,"number":3431,"title":"Add addNotificationBlock(_:) to Object","user":{"login":"icanzilb","id":633862,"avatar_url":"https://avatars.githubusercontent.com/u/633862?v=3","gravatar_id":"","url":"https://api.github.com/users/icanzilb","html_url":"https://github.com/icanzilb","followers_url":"https://api.github.com/users/icanzilb/followers","following_url":"https://api.github.com/users/icanzilb/following{/other_user}","gists_url":"https://api.github.com/users/icanzilb/gists{/gist_id}","starred_url":"https://api.github.com/users/icanzilb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/icanzilb/subscriptions","organizations_url":"https://api.github.com/users/icanzilb/orgs","repos_url":"https://api.github.com/users/icanzilb/repos","events_url":"https://api.github.com/users/icanzilb/events{/privacy}","received_events_url":"https://api.github.com/users/icanzilb/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/S:Idea%20Backlog","name":"S:Idea Backlog","color":"dedede"},{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/T:Enhancement","name":"T:Enhancement","color":"bfe5bf"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"2016-04-13T20:14:13Z","updated_at":"2016-07-01T04:43:33Z","closed_at":null,"body":"## Goals\r\n\r\nBe notified when any of an Object's properties changes value. \r\n\r\nThere are `addNotificationBlock` methods on `Results` and `List`, which makes reacting to value changes very easy. For observing changes to an `Object` you got to use KVO, which is somewhat clunky should you not use a 3rd party lib for it.\r\n\r\nI think there is a valid use case for adding `addNotificationBlock` to `Object` - especially when you are having a view controller that shows the information contained in a single object. For example in a Master-Detail application when you select one item in the master view controller you would have the following screen show all the data of a realm object (should that be how the app works). Now while the user has this screen open reacting to changes of the represented object means to observe each of the properties of that object. (Or the object representing the data in a single table cell, etc.)\r\n\r\nI can see a complication in the implementation, which is the case in which the object gets deleted while having an active notification block attached - I think in that case calling the notification block one last time before the object is released with a `nil` value would suffice to provide means for the app to react to that event.\r\n\r\n## Expected Results\r\n\r\nA view controllers `viewDidLoad` might look like this to always show live data on screen:\r\n\r\n```swift\r\nlet luvStock = realm.objects(Stock).filter(\"symbol='LUV'\").first!\r\nself.updateUIWithStock(luvStock)\r\ntoken = luvStock.addNotificationBlock {[weak self]latestStock, error in\r\n  guard let `self` = self else {return}\r\n  if let latestStock = latestStock {\r\n    self.updateUIWithStock(luvStock)\r\n  } else {\r\n    self.popViewControllerAnimated(true)\r\n  }\r\n}\r\n```\r\n\r\nThe example will fetch one `Stock` object from Realm, the update the UI any time any of the values has been updated in the background from the network controller, and finally if for whatever reason the object gets deleted it will just pop back to the previous screen."},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/229852674","html_url":"https://github.com/realm/realm-cocoa/issues/3431#issuecomment-229852674","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3431","id":229852674,"user":{"login":"JadenGeller","id":4544152,"avatar_url":"https://avatars.githubusercontent.com/u/4544152?v=3","gravatar_id":"","url":"https://api.github.com/users/JadenGeller","html_url":"https://github.com/JadenGeller","followers_url":"https://api.github.com/users/JadenGeller/followers","following_url":"https://api.github.com/users/JadenGeller/following{/other_user}","gists_url":"https://api.github.com/users/JadenGeller/gists{/gist_id}","starred_url":"https://api.github.com/users/JadenGeller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JadenGeller/subscriptions","organizations_url":"https://api.github.com/users/JadenGeller/orgs","repos_url":"https://api.github.com/users/JadenGeller/repos","events_url":"https://api.github.com/users/JadenGeller/events{/privacy}","received_events_url":"https://api.github.com/users/JadenGeller/received_events","type":"User","site_admin":false},"created_at":"2016-07-01T04:43:33Z","updated_at":"2016-07-01T04:43:33Z","body":"No need to prevent it—you can easily distinguish the initial callback from any updates. If you're using Realm Swift, just ignore the `Initial` enum case. If you're using Realm Objective-C, just ignore the case where the `RLMCollectionChange *` parameter is `nil`."}}
{"actor":{"display_login":"JadenGeller"}}