Swift 将本地领域转换为同步领域:';自定义模式&x27;无法访问
我在文档中读到了这一部分,我在Objective-C中找到了这一部分,但我无法在Swift中完全实现的应用程序上实现它Swift 将本地领域转换为同步领域:';自定义模式&x27;无法访问,swift,realm,realm-mobile-platform,Swift,Realm,Realm Mobile Platform,我在文档中读到了这一部分,我在Objective-C中找到了这一部分,但我无法在Swift中完全实现的应用程序上实现它 var syncConfig = Realm.Configuration() syncConfig.syncConfiguration = SyncConfiguration(user: user, realmURL: server.appendingPathComponent("/~/app1")) syncConfig.customSchema = localRealm.s
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 Realm.Private
,但没有解决问题
我是否应该明确使用Objective-C进行此操作?没有公共
customSchema
属性,您可以随时参考文档(了解什么是公共用途的最佳方法):
由于没有更好的选择,我决定在Swift项目中使用Objective-C。因此,我添加了一个
SWIFT\u OBJC\u bridgeing\u头
(当您添加Objective-C文件时,Xcode会自动执行此操作),并创建了一个RealmConverter
对象:
RealmConverter.h
#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
已在Swift中找到该功能的端口:
导入域
导入域。动态
导入RealmSwift
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:任何]?)->Bool{
让sourceFilePath=Bundle.main.url(对于资源:“fieldFlow”,扩展名为“realm”)
let configuration=RLMRealmConfiguration()
configuration.fileURL=sourceFilePath
configuration.dynamic=true
configuration.readOnly=true
让localRealm=try!RLMRealm(配置:配置)
让creds=SyncCredentials.usernamePassword(用户名:admin@realm.io,密码:“password”)
SyncUser.logIn(使用:creds,服务器:URL(字符串):http://localhost:9080中的(“”!){(同步用户,错误)
DispatchQueue.main.async{
如果让syncUser=syncUser{
self.copyToSyncRealmWithRealm(领域:localRealm,用户:syncUser)
}
}
}
}
func copyToSyncRealmWithRealm(领域:RLMRealm,用户:RLMSyncUser){
让syncConfig=RLMRealmConfiguration()
syncConfig.syncConfiguration=RLMSyncConfiguration(用户:用户,realmURL:URL(字符串:realm://localhost:9080/~/fieldRow“)!)
syncConfig.customSchema=realm.schema
让syncRealm=try!RLMRealm(配置:syncConfig)
syncRealm.schema=syncConfig.customSchema!
试试!syncRealm.transaction{
让objectSchema=syncConfig.customSchema!.objectSchema
对于objectSchema中的架构{
让allObjects=realm.allObjects(schema.className)
谢谢你的回答。那么食谱为什么要用它呢?
#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")
}
import Realm
import Realm.Dynamic
import RealmSwift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let sourceFilePath = Bundle.main.url(forResource: "fieldFlow", withExtension: "realm")
let configuration = RLMRealmConfiguration()
configuration.fileURL = sourceFilePath
configuration.dynamic = true
configuration.readOnly = true
let localRealm = try! RLMRealm(configuration: configuration)
let creds = SyncCredentials.usernamePassword(username: "admin@realm.io", password: "password")
SyncUser.logIn(with: creds, server: URL(string: "http://localhost:9080")!) { (syncUser, error) in
DispatchQueue.main.async {
if let syncUser = syncUser {
self.copyToSyncRealmWithRealm(realm: localRealm, user: syncUser)
}
}
}
}
func copyToSyncRealmWithRealm(realm: RLMRealm, user: RLMSyncUser) {
let syncConfig = RLMRealmConfiguration()
syncConfig.syncConfiguration = RLMSyncConfiguration(user: user, realmURL: URL(string: "realm://localhost:9080/~/fieldRow")!)
syncConfig.customSchema = realm.schema
let syncRealm = try! RLMRealm(configuration: syncConfig)
syncRealm.schema = syncConfig.customSchema!
try! syncRealm.transaction {
let objectSchema = syncConfig.customSchema!.objectSchema
for schema in objectSchema {
let allObjects = realm.allObjects(schema.className)
for i in 0..<allObjects.count {
let object = allObjects[i]
RLMCreateObjectInRealmWithValue(syncRealm, schema.className, object, true)
}
}
}
}