我在我的应用程序中获得了登录和注销方法。当用户第一次注册时,firebase 会自动生成一个唯一的,我将其存储在 userdefaults 中:
let token = user?.uid
UserDefaults.standard.set(token, forKey: KEY_UID)
当第一次发生这种情况时,我在下一个 ViewController 中打印 userdefault 和 CURRENT_USER:KEY_UID 和 CURRENT_USER 都打印正确。
print("THE CURRENT USER IS: \(CURRENT_USER)")
print("THE UNIQUE ID IS: \(UserDefaults.standard.value(forKey: KEY_UID))")
我有一个 constants.swift 文件,其中我将 userdefault: KEY_UID 等于 var CURRENT_USER,这样我就可以在我的应用程序中全局使用它。
// USER DEFAULTS
var CURRENT_USER = UserDefaults.standard.value(forKey: KEY_UID)
当我单击退出按钮并尝试再次登录时出现问题:
@IBAction func signOutBtnPressed(_ sender: AnyObject) {
CURRENT_USER = nil
UserDefaults.standard.removeObject(forKey: KEY_UID)
try! FIRAuth.auth()!.signOut()
performSegue(withIdentifier: "logoutSegue", sender: nil)
}
- 当我再次登录时,我再次获取 user.uid 并为其设置新的 userDefault:
// Login Button Pressed
@IBAction func loginBtnPressed(_ sender: AnyObject) {
FIRAuth.auth()?.signIn(withEmail: email, password: pwd, completion: { (user, error) in
if error == nil {
let token = user?.uid
UserDefaults.standard.synchronize()
CURRENT_USER = UserDefaults.standard.value(forKey: KEY_UID)
self.perform(#selector(ViewController.goToMuscleListVC), with: 1, afterDelay: 2)
}else{
self.showErrorAlert(title: "Wrong Email or Password", msg: "Please fill in a valid account or create a new account on the bottom.")
}
}) // end sign in
编辑
必须对 REF_WORKOUT_USER 创建新的引用,因为旧的引用仍然包含旧的 CURRENT_USER 值。
现在奇怪的部分是,当我尝试在下一个 ViewController 上打印 CURRENT_USER 和 KEY_UID 时,它确实将 KEY_UID 和 CURRENT_USER 打印到新登录帐户的新值。当我对 firebase 进行新调用时:
private var _REF_WORKOUT_USER = DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts")
这个 CURRENT_USER 仍然是旧用户,我不知道这是怎么可能的。
我现在做错了什么?为什么当我重新启动应用程序时它会起作用?
编辑:答案 新功能:
// Creating account on firebase with email and password
func createFirebaseDBUser(uid: String, userData: Dictionary<String, String>) {
REF_USERS.child(uid).updateChildValues(userData)
}
// Creates a workout for the user with the exercises selected in exerciseVC.swift
func CreateWorkoutForUser(baseValues: Dictionary<String, Int>, exerciseNames: [String]){
print("CURRENT USER before making an workout: \(CURRENT_USER)")
// grabbing the unique key thats generated at button click due firebase
let uniqueKey = DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts").childByAutoId().key
// appending done and value false to that unique id
DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts").child(uniqueKey).child("Done").setValue("false")
// iterate trought the exercises and for each key:value in the baseValues array set the key:value for the unique id firebase sets on the top
for ex in exerciseNames {
for (myKey, myValue) in baseValues {
DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts").child(uniqueKey).child("exercise").child(ex).child(myKey).setValue(myValue)
}
}
}
我真的很感谢任何帮助的人!
干杯。 :D
最佳答案
在删除和设置对象后尝试同步 UserDefaults:
UserDefaults.standard.set(token, forKey: KEY_UID)
UserDefaults.standard.synchronize()
关于ios - 当前用户重新登录后 Firebase 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40243968/