{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/4029","repository_url":"https://api.github.com/repos/realm/realm-cocoa","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4029/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4029/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4029/events","html_url":"https://github.com/realm/realm-cocoa/issues/4029","id":174316715,"number":4029,"title":"Add support for adding tags to RLMResult","user":{"login":"crrobinson14","id":1675563,"avatar_url":"https://avatars.githubusercontent.com/u/1675563?v=3","gravatar_id":"","url":"https://api.github.com/users/crrobinson14","html_url":"https://github.com/crrobinson14","followers_url":"https://api.github.com/users/crrobinson14/followers","following_url":"https://api.github.com/users/crrobinson14/following{/other_user}","gists_url":"https://api.github.com/users/crrobinson14/gists{/gist_id}","starred_url":"https://api.github.com/users/crrobinson14/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crrobinson14/subscriptions","organizations_url":"https://api.github.com/users/crrobinson14/orgs","repos_url":"https://api.github.com/users/crrobinson14/repos","events_url":"https://api.github.com/users/crrobinson14/events{/privacy}","received_events_url":"https://api.github.com/users/crrobinson14/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-08-31T16:18:53Z","updated_at":"2016-08-31T16:28:03Z","closed_at":null,"body":"## Goals\r\n\r\nAdd support for adding a tag to a RLMResult collection.\r\n\r\n## Expected Results\r\n\r\nNotifications are a HUGE improvement in terms of keeping UITableViews up to date with changes in a RLMResults collection. However, it's a little tricky to reuse code between them. It works fine for simple tables with one section, or hard-coded cases where the sections are known. But I find myself repeating a lot of nearly-identical notification-listener code between controllers.\r\n\r\nWhat I would love to see is the ability to add a tag to the RLMResult object after obtaining it. I could then make a generic \"updateTableViewSection\" method in a utility class or category and pass it the notification received, a reference to the tableview, and the result set.\r\n\r\nMy primary purpose is to provide a more generic way to track a data set tied to a tableview section to pass to the `RLMCollectionChange ...InSection` helper methods. But this is just one use-case - I could imagine that tags would be useful for other cases as well.\r\n\r\n## Code Sample\r\n\r\nThis is the block I'm repeating everywhere. It's dupe code across viewcontrollers, which is ugly but tolerable. But in viewcontrollers with multiple tableview sections, especially where those sections come from different queries, it quickly becomes a LOT of duplicated code.\r\n\r\n```\r\n    __weakSelf weakSelf = self;\r\n    _notificationToken1 = [self.unreadShares addNotificationBlock:^(RLMResults *data, RLMCollectionChange *changes, NSError *error) {\r\n        if (error) {\r\n            NSLog(@\"Failed to open Realm on background worker: %@\", error);\r\n            return;\r\n        }\r\n\r\n        UITableView *tableView = weakSelf.tableView;\r\n\r\n        // Initial run of the query will pass nil for the change information\r\n        if (!changes) {\r\n            [tableView reloadData];\r\n            return;\r\n        }\r\n\r\n        // changes is non-nil, so we just need to update the tableview\r\n        [tableView beginUpdates];\r\n        [tableView deleteRowsAtIndexPaths:[changes deletionsInSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n        [tableView insertRowsAtIndexPaths:[changes insertionsInSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n        [tableView reloadRowsAtIndexPaths:[changes modificationsInSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n        [tableView endUpdates];\r\n    }];\r\n```\r\n\r\nWhat I would love to do:\r\n\r\n```\r\n+ (void)processRealmTVUpdates:(UITableView *)tableView withResults:(RLMResults *)results andChangeset:(RLMCollectionChange *)changes error:(NSError *)error {\r\n    NSUInteger section = [results.tag integerValue];\r\n\r\n    if (error) {\r\n        NSLog(@\"Failed to open Realm on background worker: %@\", error);\r\n        return;\r\n    }\r\n\r\n    // Initial run of the query will pass nil for the change information\r\n    if (!changes) {\r\n        [tableView reloadData];\r\n        return;\r\n    }\r\n\r\n    // changes is non-nil, so we just need to update the tableview\r\n    [tableView beginUpdates];\r\n    [tableView deleteRowsAtIndexPaths:[changes deletionsInSection:section] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n    [tableView insertRowsAtIndexPaths:[changes insertionsInSection:section] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n    [tableView reloadRowsAtIndexPaths:[changes modificationsInSection:section] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n    [tableView endUpdates];\r\n}\r\n\r\n- (void)viewDidLoad {\r\n    RLMResults *results = [MyType somePredefinedQuery];\r\n    results.tag = @0;\r\n\r\n    __weakSelf weakSelf = self;\r\n    token = [self.unreadShares addNotificationBlock:^(RLMResults *data, RLMCollectionChange *changes, NSError *error) {\r\n        [MyHelperClass processRealmTVUpdates:weakSelf.tableView withResults:data andChangeset:changes error:error];\r\n    }];\r\n}\r\n```\r\n\r\nI probably have a thousand lines of this boilerplate in the app I'm currently working on."},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/243820799","html_url":"https://github.com/realm/realm-cocoa/issues/4029#issuecomment-243820799","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4029","id":243820799,"user":{"login":"tgoyne","id":408944,"avatar_url":"https://avatars.githubusercontent.com/u/408944?v=3","gravatar_id":"","url":"https://api.github.com/users/tgoyne","html_url":"https://github.com/tgoyne","followers_url":"https://api.github.com/users/tgoyne/followers","following_url":"https://api.github.com/users/tgoyne/following{/other_user}","gists_url":"https://api.github.com/users/tgoyne/gists{/gist_id}","starred_url":"https://api.github.com/users/tgoyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tgoyne/subscriptions","organizations_url":"https://api.github.com/users/tgoyne/orgs","repos_url":"https://api.github.com/users/tgoyne/repos","events_url":"https://api.github.com/users/tgoyne/events{/privacy}","received_events_url":"https://api.github.com/users/tgoyne/received_events","type":"User","site_admin":false},"created_at":"2016-08-31T16:28:03Z","updated_at":"2016-08-31T16:28:03Z","body":"You can just capture the tag in the notification block and explicitly pass it in to your helper:\r\n\r\n    + (void)processRealmTVUpdates:(UITableView *)tableView withResults:(RLMResults *)results andChangeset:(RLMCollectionChange *)changes section:(NSUInteger)section error:(NSError *)error {\r\n        if (error) {\r\n            NSLog(@\"Failed to open Realm on background worker: %@\", error);\r\n            return;\r\n        }\r\n\r\n        // Initial run of the query will pass nil for the change information\r\n        if (!changes) {\r\n            [tableView reloadData];\r\n            return;\r\n        }\r\n\r\n        // changes is non-nil, so we just need to update the tableview\r\n        [tableView beginUpdates];\r\n        [tableView deleteRowsAtIndexPaths:[changes deletionsInSection:section] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n        [tableView insertRowsAtIndexPaths:[changes insertionsInSection:section] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n        [tableView reloadRowsAtIndexPaths:[changes modificationsInSection:section] withRowAnimation:UITableViewRowAnimationAutomatic];\r\n        [tableView endUpdates];\r\n    }\r\n\r\n    - (void)viewDidLoad {\r\n        RLMResults *results = [MyType somePredefinedQuery];\r\n        NSUInteger section = 0;\r\n\r\n        __weakSelf weakSelf = self;\r\n        token = [self.unreadShares addNotificationBlock:^(RLMResults *data, RLMCollectionChange *changes, NSError *error) {\r\n            [MyHelperClass processRealmTVUpdates:weakSelf.tableView withResults:data andChangeset:changes section:section error:error];\r\n        }];\r\n    }\r\n"}}
{"actor":{"display_login":"tgoyne"}}