나는 내 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()
}
그러나 저는이 방법이 더 해킹 작업으로 만족스럽지 않습니다.
다중 사용자 로그인 시나리오를 수행하는 가장 좋은 방법은 무엇입니까?