2016-11-19 7 views
0

私は自分の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() 
} 

しかし、私はこのアプローチに満足しているとは言えません。

マルチユーザーログインシナリオを実行する最良の方法は何ですか?

答えて

1

デフォルト構成が指しているRealmファイルを継続的に変更することは、おそらく最大ではありません。 Realm自体は、パフォーマンス上の理由からファイルへの参照を内部的にキャッシュするため、ファイルが実際に開かれた後に構成を変更することはお勧めしません。

複数のユーザーが管理するシステムでは、ユーザーごとに1つのレルムファイルを持つことは間違いありません。

コードアーキテクチャレベルでは、現在のユーザーの状態を管理するシングルトンオブジェクトを持つことが適切であると考えており、必要なときにいつでも適切に書式設定されたConfigurationオブジェクトを提供します。

class User { 
    static let currentUser = User() 
    private var userID: String? = nil 

    public var configuration: Realm.Configuration { 
     let configuration = Realm.Configuration() 
     configuration.fileURL = URL(filePath: "\(userID).realm") 
     return configuration 
    } 

    public func logIn(withUserID userID: String) { 
     self.userID = userID 
    } 

    public func logOut() { 
     self.userID = nil 
    } 
} 

let userRealm = try! Realm(configuration: User.currentUser.configuration)