Realm Swift多用户登录

时间:2016-11-19 11:48:25

标签: ios swift realm

我在我的一个iOS项目中使用Realm Swift,其中一个应用程序要求是允许多个用户的数据共存。当相同的用户登录时,我遇到问题,因为Realm无法识别与该用户关联的领域db文件。

例如:因此每次UserA在注销后重新登录时,都会为UserA生成一个新的Realm文件。当UserA注销并且UserB登录,然后UserB注销并且UserA登录时,就不会发生这种情况。

UserA(注销) - > UserB(登录) - > UseB(注销) - > UserA(登录)[本作品]

UserA(登录) - > UserA(注销) - > UserA(登录)[这不起作用,创建了一个新的Realm文件,如果存在迁移,请尝试! 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()
}

更新:我在加载用户域配置之前加载了默认的Realm,使其暂时工作,如下面的代码所示:

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 个答案:

答案 0 :(得分:1)

持续更改默认配置指向的Realm文件可能不是最好的。出于性能原因,Realm本身会在内部缓存对文件的引用,因此不建议在文件实际打开后更改配置。

对于多用户管理系统,每个用户拥有一个Realm文件肯定是有意义的。

在代码体系结构级别上,我认为拥有一个管理当前用户状态的单例对象,并在需要时提供格式正确的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)