AppDelegate.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 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 "AppDelegate.h"
  19. #import "Tick.h"
  20. @interface TickViewController : UIViewController
  21. @property (nonatomic, strong) UIButton *button;
  22. @property (nonatomic, strong) Tick *tick;
  23. @property (nonatomic, strong) RLMNotificationToken *notificationToken;
  24. @end
  25. @implementation AppDelegate
  26. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  27. RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
  28. configuration.fileURL = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.io.realm.examples.extension"] URLByAppendingPathComponent:@"extension.realm"];
  29. [RLMRealmConfiguration setDefaultConfiguration:configuration];
  30. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  31. self.window.rootViewController = [[TickViewController alloc] init];
  32. [self.window makeKeyAndVisible];
  33. return YES;
  34. }
  35. @end
  36. @implementation TickViewController
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. self.tick = [Tick allObjects].firstObject;
  40. if (!self.tick) {
  41. [[RLMRealm defaultRealm] transactionWithBlock:^{
  42. self.tick = [Tick createInDefaultRealmWithValue:@[@"", @0]];
  43. }];
  44. }
  45. self.notificationToken = [self.tick.realm addNotificationBlock:^(NSString *notification, RLMRealm *realm) {
  46. // Occasionally, respond immediately to the notification by triggering a new notification.
  47. if (self.tick.count % 13 == 0) {
  48. [self tock];
  49. }
  50. [self updateLabel];
  51. }];
  52. self.button = [UIButton buttonWithType:UIButtonTypeSystem];
  53. self.button.frame = self.view.bounds;
  54. [self.button addTarget:self action:@selector(tock) forControlEvents:UIControlEventTouchUpInside];
  55. self.button.titleLabel.textAlignment = NSTextAlignmentCenter;
  56. [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  57. [self.view addSubview:self.button];
  58. self.view.backgroundColor = [UIColor purpleColor];
  59. [self updateLabel];
  60. }
  61. - (void)viewWillAppear:(BOOL)animated {
  62. [super viewWillAppear:animated];
  63. self.button.frame = self.view.bounds;
  64. [self updateLabel];
  65. }
  66. - (void)viewDidAppear:(BOOL)animated {
  67. [super viewDidAppear:animated];
  68. [self tock];
  69. [self updateLabel];
  70. }
  71. - (void)updateLabel {
  72. [self.button setTitle:@(self.tick.count).stringValue forState:UIControlStateNormal];
  73. }
  74. - (void)tock {
  75. [[RLMRealm defaultRealm] transactionWithBlock:^{
  76. self.tick.count++;
  77. }];
  78. }
  79. @end