Swift 将本地域转换为同步域
我想将本地域数据库转换为同步数据库,以允许用户身份验证。我使用的是swift,它没有包含在文档中,然而,我发现了这个方法,但它一直给SIGABRT异常,我不知道是什么问题 以下是我在App Delegate中添加的内容:Swift 将本地域转换为同步域,swift,realm,Swift,Realm,我想将本地域数据库转换为同步数据库,以允许用户身份验证。我使用的是swift,它没有包含在文档中,然而,我发现了这个方法,但它一直给SIGABRT异常,我不知道是什么问题 以下是我在App Delegate中添加的内容: import UIKit import RealmSwift import Realm import Realm.Dynamic @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate
import UIKit
import RealmSwift
import Realm
import Realm.Dynamic
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
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)
}
}
}
let config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1,
// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()
// Override point for customization after application launch.
return true
}
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)
}
}
}
}
导入UIKit
导入RealmSwift
进口领域
导入域。动态
@UIApplicationMain
类AppDelegate:UIResponder、UIApplicationLegate{
变量窗口:UIWindow?
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)
}
}
}
让config=Realm.Configuration(
//设置新架构版本。该版本必须大于以前使用的版本
//版本(如果以前从未设置过架构版本,则版本为0)。
阴谋厌恶:1,
//设置使用打开域时自动调用的块
//低于上面设置的架构版本
migrationBlock:{migration,oldSchemaVersion in
//我们还没有迁移任何内容,所以oldSchemaVersion==0
if(oldschemaversation<1){
//无事可做!
//领域将自动检测新属性和删除的属性
//并将自动更新磁盘上的架构
}
})
//告诉领域将此新配置对象用于默认领域
Realm.Configuration.defaultConfiguration=config
//现在我们已经告诉Realm如何处理模式更改,打开文件
//将自动执行迁移
让realm=try!realm()
//应用程序启动后自定义的覆盖点。
返回真值
}
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)
对于0中的i..可能您无法将本地域作为同步域打开,反之亦然。我发现一些参考资料说,这是可能的。如果您似乎没有按同步域的方式打开它,那么您正在复制数据-如果本地域在其架构中包含同步域所包含的所有类,那么这是可能的