私は自分のiOSプロジェクトでRealm Swiftを使用しています。アプリケーション要件の1つは、複数のユーザーのデータを共存させることです。 Realmがそのユーザーに関連付けられている領域データベースファイルを識別できないため、同じユーザーがログインしたときに問題が発生しています。Realm Swift Multi User Login
例:ログアウト後にUserAが再度ログインするたびに、UserAの新しいRealmファイルが生成されます。 UserAがログアウトしてUserBがログインした後に、UserBがログアウトしてUserAがログインしたときには発生しません。
UserA(ログアウト) - > UserB(ログイン) - > UseB(ログアウト) - > UserA(ログイン)[This [UserA(ログイン)] - > UserA(ログイン)[これは動作しません。新しいRealmファイルが作成され、既存の移行が存在する場合は試してください!レルム()も失敗します]
私のアプリケーション内のAppDelegateコード:didFinishLaunchingWithOptionsは次のようになります。
func setDefaultRealmForUser() {
var config = Realm.Configuration()
// Inside your application(application:didFinishLaunchingWithOptions:)
let currentLoggedInRegId = NSUserDefaults.standardUserDefaults().valueForKey(Constants.UserDefaults.CurrentLoggedInRegId)
if currentLoggedInRegId != nil {
let registrationId = currentLoggedInRegId as! String
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.URLByDeletingLastPathComponent?
.URLByAppendingPathComponent("\(registrationId).realm")
}
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
}
と成功の私のloginViewControllerコードは、次の
func setDefaultRealmForUser(onComplete:()->()) {
var config = Realm.Configuration()
let currentLoggedInRegId = NSUserDefaults.standardUserDefaults().valueForKey(Constants.UserDefaults.CurrentLoggedInRegId)
if currentLoggedInRegId != nil {
let registrationId = currentLoggedInRegId as! String
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.URLByDeletingLastPathComponent?
.URLByAppendingPathComponent("\(registrationId).realm")
}
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
onComplete()
}
アップデートのようになります。私はそれが見えるユーザーレルムの設定を、ロードする前に、デフォルトのレルムをロードすることによって、当面のために働く作りました次のコード:
func reloadRealmWithDefault(onComplete:()->()) -> (Void) {
var config = Realm.Configuration()
let defaultRealm = "default"
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.URLByDeletingLastPathComponent?
.URLByAppendingPathComponent("\(defaultRealm).realm")
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
onComplete()
}
しかし、私はこのアプローチに満足しているとは言えません。
マルチユーザーログインシナリオを実行する最良の方法は何ですか?