Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var syncConfig = Realm.Configuration()
- syncConfig.syncConfiguration = SyncConfiguration(user: user, realmURL: server.appendingPathComponent("/~/app1"))
- syncConfig.customSchema = localRealm.schema
- ~~~~~~~~~~~~~~~~~~~~~~~
- ^ 'customSchema' is inaccessible due to 'private' protection level
- #import <Foundation/Foundation.h>
- @import Realm;
- NS_ASSUME_NONNULL_BEGIN
- @interface RealmConverter : NSObject
- - (void)convertLocalToSyncRealm:(NSURL *)server local:(NSURL *)local username:(NSString *)username password:(NSString *)password completion:(void (^)(NSError * _Nullable))completion;
- @end
- NS_ASSUME_NONNULL_END
- #import "RealmConverter.h"
- @import Realm.Dynamic;
- @import Realm.Private;
- @implementation RealmConverter
- - (void)convertLocalToSyncRealm:(NSURL *)server local:(NSURL *)local username:(NSString *)username password:(NSString *)password completion:(void (^)(NSError * _Nullable))completion {
- RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
- configuration.fileURL = local;
- configuration.dynamic = true;
- configuration.readOnly = YES;
- RLMRealm *localRealm = [RLMRealm realmWithConfiguration:configuration error:nil];
- RLMSyncCredentials *credentials = [RLMSyncCredentials credentialsWithUsername:username password:password register:YES];
- [RLMSyncUser logInWithCredentials:credentials authServerURL:server onCompletion:^(RLMSyncUser *syncUser, NSError *error) {
- if (error) {
- completion(error);
- return;
- }
- dispatch_async(dispatch_get_main_queue(), ^{
- RLMRealmConfiguration *syncConfig = [[RLMRealmConfiguration alloc] init];
- syncConfig.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:syncUser realmURL:[NSURL URLWithString:[NSString stringWithFormat:@"realm://%@:%@/~/<redacted>", server.host, server.port]]];
- syncConfig.customSchema = [localRealm.schema copy];
- RLMRealm *syncRealm = [RLMRealm realmWithConfiguration:syncConfig error:nil];
- syncRealm.schema = syncConfig.customSchema;
- NSError *error = nil;
- [syncRealm transactionWithBlock:^{
- NSArray *objectSchema = syncConfig.customSchema.objectSchema;
- for (RLMObjectSchema *schema in objectSchema) {
- RLMResults *allObjects = [localRealm allObjects:schema.className];
- for (RLMObject *object in allObjects) {
- RLMCreateObjectInRealmWithValue(syncRealm, schema.className, object, true);
- }
- }
- completion(nil);
- } error:&error];
- if (error) {
- completion(error);
- }
- });
- }];
- }
- @end
- RealmConverter().convertLocal(toSyncRealm: URL(string: "http://localhost:9080")!, local: Realm.Configuration.defaultConfiguration.fileURL!, username: "user@example.com", password: "12345678") { error in
- print("Done:", error ?? "nil")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement