AppDelegate.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #import "DataModels.h"
  21. @implementation AppDelegate
  22. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  23. {
  24. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  25. self.window.rootViewController = [[UIViewController alloc] init];
  26. [self.window makeKeyAndVisible];
  27. // copy over old data files for migration
  28. NSURL *defaultRealmURL = [RLMRealmConfiguration defaultConfiguration].fileURL;
  29. NSURL *defaultRealmParentURL = [defaultRealmURL URLByDeletingLastPathComponent];
  30. NSURL *v0URL = [[NSBundle mainBundle] URLForResource:@"default-v0" withExtension:@"realm"];
  31. [[NSFileManager defaultManager] removeItemAtURL:defaultRealmURL error:nil];
  32. [[NSFileManager defaultManager] copyItemAtURL:v0URL toURL:defaultRealmURL error:nil];
  33. // trying to open an outdated realm file without first registering a new schema version and migration block
  34. // with throw
  35. @try {
  36. [RLMRealm defaultRealm];
  37. }
  38. @catch (NSException *exception) {
  39. NSLog(@"Trying to open an outdated realm a migration block threw an exception.");
  40. }
  41. // define a migration block
  42. // you can define this inline, but we will reuse this to migrate realm files from multiple versions
  43. // to the most current version of our data model
  44. RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
  45. if (oldSchemaVersion < 1) {
  46. [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
  47. if (oldSchemaVersion < 1) {
  48. // combine name fields into a single field
  49. newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", oldObject[@"firstName"], oldObject[@"lastName"]];
  50. }
  51. }];
  52. }
  53. if (oldSchemaVersion < 2) {
  54. [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
  55. // give JP a dog
  56. if ([newObject[@"fullName"] isEqualToString:@"JP McDonald"]) {
  57. Pet *jpsDog = [[Pet alloc] initWithValue:@[@"Jimbo", @(AnimalTypeDog)]];
  58. [newObject[@"pets"] addObject:jpsDog];
  59. }
  60. }];
  61. }
  62. if (oldSchemaVersion < 3) {
  63. [migration enumerateObjects:Pet.className block:^(RLMObject *oldObject, RLMObject *newObject) {
  64. // convert type string to type enum if we have outdated Pet object
  65. if (oldObject && oldObject.objectSchema[@"type"].type == RLMPropertyTypeString) {
  66. newObject[@"type"] = @([Pet animalTypeForString:oldObject[@"type"]]);
  67. }
  68. }];
  69. }
  70. NSLog(@"Migration complete.");
  71. };
  72. RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
  73. // set the schema verion and migration block for the defualt realm
  74. configuration.schemaVersion = 3;
  75. configuration.migrationBlock = migrationBlock;
  76. [RLMRealmConfiguration setDefaultConfiguration:configuration];
  77. // now that we have set a schema version and migration block for the configuration,
  78. // performing the migration and opening the Realm will succeed
  79. [RLMRealm defaultRealm];
  80. // print out all migrated objects in the default realm
  81. NSLog(@"Migrated objects in the default Realm: %@", [[Person allObjects] description]);
  82. //
  83. // Migrate other Realm versions
  84. //
  85. NSURL *v1URL = [[NSBundle mainBundle] URLForResource:@"default-v1" withExtension:@"realm"];
  86. NSURL *v2URL = [[NSBundle mainBundle] URLForResource:@"default-v2" withExtension:@"realm"];
  87. NSURL *realmv1URL = [defaultRealmParentURL URLByAppendingPathComponent:@"default-v1.realm"];
  88. NSURL *realmv2URL = [defaultRealmParentURL URLByAppendingPathComponent:@"default-v2.realm"];
  89. [[NSFileManager defaultManager] removeItemAtURL:realmv1URL error:nil];
  90. [[NSFileManager defaultManager] copyItemAtURL:v1URL toURL:realmv1URL error:nil];
  91. [[NSFileManager defaultManager] removeItemAtURL:realmv2URL error:nil];
  92. [[NSFileManager defaultManager] copyItemAtURL:v2URL toURL:realmv2URL error:nil];
  93. // set schemave versions and migration blocks form Realms at each path
  94. RLMRealmConfiguration *realmv1Configuration = [configuration copy];
  95. realmv1Configuration.fileURL = realmv1URL;
  96. RLMRealmConfiguration *realmv2Configuration = [configuration copy];
  97. realmv2Configuration.fileURL = realmv2URL;
  98. // manully migration v1Path, v2Path is migrated implicitly on access
  99. [RLMRealm performMigrationForConfiguration:realmv1Configuration error:nil];
  100. // print out all migrated objects in the migrated realms
  101. RLMRealm *realmv1 = [RLMRealm realmWithConfiguration:realmv1Configuration error:nil];
  102. NSLog(@"Migrated objects in the Realm migrated from v1: %@", [[Person allObjectsInRealm:realmv1] description]);
  103. RLMRealm *realmv2 = [RLMRealm realmWithConfiguration:realmv2Configuration error:nil];
  104. NSLog(@"Migrated objects in the Realm migrated from v2: %@", [[Person allObjectsInRealm:realmv2] description]);
  105. return YES;
  106. }
  107. @end