Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. var syncConfig = Realm.Configuration()
  2. syncConfig.syncConfiguration = SyncConfiguration(user: user, realmURL: server.appendingPathComponent("/~/app1"))
  3. syncConfig.customSchema = localRealm.schema
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. ^ 'customSchema' is inaccessible due to 'private' protection level
  6.  
  7. #import <Foundation/Foundation.h>
  8.  
  9. @import Realm;
  10.  
  11. NS_ASSUME_NONNULL_BEGIN
  12.  
  13. @interface RealmConverter : NSObject
  14.  
  15. - (void)convertLocalToSyncRealm:(NSURL *)server local:(NSURL *)local username:(NSString *)username password:(NSString *)password completion:(void (^)(NSError * _Nullable))completion;
  16.  
  17. @end
  18.  
  19. NS_ASSUME_NONNULL_END
  20.  
  21. #import "RealmConverter.h"
  22.  
  23. @import Realm.Dynamic;
  24. @import Realm.Private;
  25.  
  26. @implementation RealmConverter
  27.  
  28. - (void)convertLocalToSyncRealm:(NSURL *)server local:(NSURL *)local username:(NSString *)username password:(NSString *)password completion:(void (^)(NSError * _Nullable))completion {
  29. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  30. configuration.fileURL = local;
  31. configuration.dynamic = true;
  32. configuration.readOnly = YES;
  33.  
  34. RLMRealm *localRealm = [RLMRealm realmWithConfiguration:configuration error:nil];
  35.  
  36. RLMSyncCredentials *credentials = [RLMSyncCredentials credentialsWithUsername:username password:password register:YES];
  37. [RLMSyncUser logInWithCredentials:credentials authServerURL:server onCompletion:^(RLMSyncUser *syncUser, NSError *error) {
  38. if (error) {
  39. completion(error);
  40. return;
  41. }
  42.  
  43. dispatch_async(dispatch_get_main_queue(), ^{
  44. RLMRealmConfiguration *syncConfig = [[RLMRealmConfiguration alloc] init];
  45. syncConfig.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:syncUser realmURL:[NSURL URLWithString:[NSString stringWithFormat:@"realm://%@:%@/~/<redacted>", server.host, server.port]]];
  46. syncConfig.customSchema = [localRealm.schema copy];
  47.  
  48. RLMRealm *syncRealm = [RLMRealm realmWithConfiguration:syncConfig error:nil];
  49. syncRealm.schema = syncConfig.customSchema;
  50.  
  51. NSError *error = nil;
  52. [syncRealm transactionWithBlock:^{
  53. NSArray *objectSchema = syncConfig.customSchema.objectSchema;
  54. for (RLMObjectSchema *schema in objectSchema) {
  55. RLMResults *allObjects = [localRealm allObjects:schema.className];
  56. for (RLMObject *object in allObjects) {
  57. RLMCreateObjectInRealmWithValue(syncRealm, schema.className, object, true);
  58. }
  59. }
  60. completion(nil);
  61. } error:&error];
  62.  
  63. if (error) {
  64. completion(error);
  65. }
  66. });
  67. }];
  68. }
  69.  
  70. @end
  71.  
  72. RealmConverter().convertLocal(toSyncRealm: URL(string: "http://localhost:9080")!, local: Realm.Configuration.defaultConfiguration.fileURL!, username: "user@example.com", password: "12345678") { error in
  73. print("Done:", error ?? "nil")
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement