AppDelegate.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "AppDelegate.h"
  19. #import <Realm/Realm.h>
  20. // Define your models
  21. @interface Dog : RLMObject
  22. @property NSString *name;
  23. @property NSInteger age;
  24. @end
  25. @implementation Dog
  26. // No need for implementation
  27. @end
  28. RLM_ARRAY_TYPE(Dog)
  29. @interface Person : RLMObject
  30. @property NSString *name;
  31. @property RLMArray<Dog> *dogs;
  32. @end
  33. @implementation Person
  34. @end
  35. @implementation AppDelegate
  36. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  37. {
  38. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  39. self.window.rootViewController = [[UIViewController alloc] init];
  40. [self.window makeKeyAndVisible];
  41. [[NSFileManager defaultManager] removeItemAtURL:[RLMRealmConfiguration defaultConfiguration].fileURL error:nil];
  42. // Create a standalone object
  43. Dog *mydog = [[Dog alloc] init];
  44. // Set & read properties
  45. mydog.name = @"Rex";
  46. mydog.age = 9;
  47. NSLog(@"Name of dog: %@", mydog.name);
  48. // Realms are used to group data together
  49. RLMRealm *realm = [RLMRealm defaultRealm]; // Create realm pointing to default file
  50. // Save your object
  51. [realm beginWriteTransaction];
  52. [realm addObject:mydog];
  53. [realm commitWriteTransaction];
  54. // Query
  55. RLMResults *results = [Dog objectsInRealm:realm where:@"name contains 'x'"];
  56. // Queries are chainable!
  57. RLMResults *results2 = [results objectsWhere:@"age > 8"];
  58. NSLog(@"Number of dogs: %li", (unsigned long)results2.count);
  59. // Link objects
  60. Person *person = [[Person alloc] init];
  61. person.name = @"Tim";
  62. [person.dogs addObject:mydog];
  63. [realm beginWriteTransaction];
  64. [realm addObject:person];
  65. [realm commitWriteTransaction];
  66. // Multi-threading
  67. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  68. @autoreleasepool {
  69. RLMRealm *otherRealm = [RLMRealm defaultRealm];
  70. RLMResults *otherResults = [Dog objectsInRealm:otherRealm where:@"name contains 'Rex'"];
  71. NSLog(@"Number of dogs: %li", (unsigned long)otherResults.count);
  72. }
  73. });
  74. return YES;
  75. }
  76. @end