2016-11-19 1 views
0

나는 내 IOS 프로젝트 중 하나에서 렐름 스위프트를 사용하고 있으며, 하나의 앱 요구 사항은 여러 사용자의 데이터가 공존 할 수있게하는 것입니다. Realm이 해당 사용자와 관련된 realm db 파일을 식별 할 수 없어 동일한 사용자가 로그인 할 때 문제가 발생합니다.렐름 스위프트 멀티 유저 로그인

예 : 로그 아웃 후 UserA가 다시 로그인 할 때마다 UserA에 대해 새로운 영역 파일이 생성됩니다. 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

기본 구성이 가리키고있는 영역 파일을 지속적으로 변경하는 것이 가장 좋은 방법은 아닙니다. 영역 자체는 성능상의 이유로 내부적으로 파일에 대한 참조를 캐시하므로 파일이 실제로 열리면 구성을 변경하지 않는 것이 좋습니다.

다중 사용자 관리 시스템의 경우 사용자 당 하나의 영역 파일을 갖는 것이 좋습니다.

코드 아키텍처 수준에서 현재 사용자의 상태를 관리하는 싱글 톤 개체를 사용하는 것이 적절할 것이라고 생각하며 필요할 때마다 적절한 형식의 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) 
관련 문제