TableViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "TableViewController.h"
  19. #import <Realm/Realm.h>
  20. // Realm model object
  21. @interface DemoObject : RLMObject
  22. @property NSString *title;
  23. @property NSDate *date;
  24. @property NSString *sectionTitle;
  25. @end
  26. @implementation DemoObject
  27. // None needed
  28. @end
  29. static NSString * const kCellID = @"cell";
  30. static NSString * const kTableName = @"table";
  31. @interface TableViewController ()
  32. @property (nonatomic, strong) NSArray *sectionTitles;
  33. @property (nonatomic, strong) NSMutableArray *objectsBySection;
  34. @property (nonatomic, strong) RLMNotificationToken *notification;
  35. @end
  36. @implementation TableViewController
  37. #pragma mark - View Lifecycle
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. // Section Titles
  42. self.sectionTitles = @[@"A", @"B", @"C"];
  43. [self setupUI];
  44. // Set realm notification block
  45. __weak typeof(self) weakSelf = self;
  46. self.notification = [RLMRealm.defaultRealm addNotificationBlock:^(NSString *note, RLMRealm *realm) {
  47. [weakSelf.tableView reloadData];
  48. }];
  49. self.objectsBySection = [NSMutableArray arrayWithCapacity:3];
  50. for (NSString *section in self.sectionTitles) {
  51. RLMResults *unsortedObjects = [DemoObject objectsWhere:@"sectionTitle == %@", section];
  52. RLMResults *sortedObjects = [unsortedObjects sortedResultsUsingKeyPath:@"date" ascending:YES];
  53. [self.objectsBySection addObject:sortedObjects];
  54. }
  55. [self.tableView reloadData];
  56. }
  57. #pragma mark - UI
  58. - (void)setupUI
  59. {
  60. self.title = @"GroupedTableView";
  61. self.navigationItem.leftBarButtonItem =
  62. [[UIBarButtonItem alloc] initWithTitle:@"BG Add"
  63. style:UIBarButtonItemStylePlain
  64. target:self
  65. action:@selector(backgroundAdd)];
  66. self.navigationItem.rightBarButtonItem =
  67. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
  68. target:self
  69. action:@selector(add)];
  70. }
  71. #pragma mark - UITableViewDataSource
  72. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  73. {
  74. return self.sectionTitles.count;
  75. }
  76. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  77. {
  78. return self.sectionTitles[section];
  79. }
  80. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  81. {
  82. return [self objectsInSection:section].count;
  83. }
  84. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  85. {
  86. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
  87. if (!cell) {
  88. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  89. reuseIdentifier:kCellID];
  90. }
  91. DemoObject *object = [self objectsInSection:indexPath.section][indexPath.row];
  92. cell.textLabel.text = object.title;
  93. cell.detailTextLabel.text = object.date.description;
  94. return cell;
  95. }
  96. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  97. forRowAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. if (editingStyle == UITableViewCellEditingStyleDelete) {
  100. RLMRealm *realm = RLMRealm.defaultRealm;
  101. [realm beginWriteTransaction];
  102. [realm deleteObject:[self objectsInSection:indexPath.section][indexPath.row]];
  103. [realm commitWriteTransaction];
  104. }
  105. }
  106. #pragma mark - Actions
  107. - (void)backgroundAdd
  108. {
  109. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  110. // Import many items in a background thread
  111. dispatch_async(queue, ^{
  112. // Get new realm and table since we are in a new thread
  113. @autoreleasepool {
  114. RLMRealm *realm = [RLMRealm defaultRealm];
  115. [realm beginWriteTransaction];
  116. for (NSInteger index = 0; index < 5; index++) {
  117. // Add row via dictionary. Order is ignored.
  118. [DemoObject createInRealm:realm withValue:@{@"title": [self randomTitle],
  119. @"date": [NSDate date],
  120. @"sectionTitle": [self randomSectionTitle]}];
  121. }
  122. [realm commitWriteTransaction];
  123. }
  124. });
  125. }
  126. - (void)add
  127. {
  128. [[RLMRealm defaultRealm] transactionWithBlock:^{
  129. [DemoObject createInDefaultRealmWithValue:@[[self randomTitle], [NSDate date], [self randomSectionTitle]]];
  130. }];
  131. }
  132. #pragma - Helpers
  133. - (RLMResults *)objectsInSection:(NSUInteger)section
  134. {
  135. return self.objectsBySection[section];
  136. }
  137. - (NSString *)randomTitle
  138. {
  139. return [NSString stringWithFormat:@"Title %d", arc4random()];
  140. }
  141. - (NSString *)randomSectionTitle
  142. {
  143. return self.sectionTitles[arc4random() % self.sectionTitles.count];
  144. }
  145. @end