缓存实现方法

时间:2018-10-12 04:19:16

标签: swift xcode firebase

我想另存为UserDefaults作为缓存。 但是

这是signupButtonTapped

@IBAction private func signUpButtonTapped(_ sender: Any) {
    KRProgressHUD.show(withMessage: "loading")
    Authentication.signUp(
        name: nameTextField.text!, // "new"
        email: emailTextField.text!, // "new"
        password: passwordTextField.text!, // "new"
        image: selectedImage,
    onSuccess: {
        KRProgressHUD.dismiss()
        API.User.getCurrentUser { (user) in
            CurrentUser.shared.synchronize(user: user)
        }
        self.presentViewController()
    }) { (error) in
        KRProgressHUD.showError(withMessage: "\(error!)")
    }
}

这是CurrentUser类:

import Foundation

final class CurrentUser: User {

// Get
static let shared: CurrentUser = {
    let user = CurrentUser()
    let userDefaults = UserDefaults.standard
    user.userID = userDefaults.string(forKey: userID_field)
    user.name = userDefaults.string(forKey: name_field)
    user.email = userDefaults.string(forKey: email_field)
    return user
}()

// Save
func synchronize(user: user) {
    print(user.name, user.userID, user.email)  // "new", "new", "new"
// <<<< BREAKPOINT

    let userDefaults = UserDefaults.standard
    userDefaults.set(user.userID, forKey: userID_field)
    userDefaults.set(user.name, forKey: name_field)
    userDefaults.set(user.email, forKey: email_field)

    print(userDefaults.string(forKey: name_field), userDefaults.string(forKey: userID_field), userDefaults.string(forKey: email_field)) // "new", "new", "new"
    print(CurrentUser.shared.name, CurrentUser.shared.userID, CurrentUser.shared.email) // "old", "old", "old"
// <<<< BREAKPOINT
}

我希望最后一个print(CurrentUser.shared.ooo)函数是“ new”,“ new”,“ new”。

但是,显示的是旧值“ old”。

我的缓存实现有什么问题?

1 个答案:

答案 0 :(得分:0)

由于您使用的是静态实例shared,因此该属性将仅保存第一个值。您需要将策略更改为其他策略,因为一旦启动应用,计算属性将不再进行计算,因此不会从用户默认值中获取数据。

您看到当您保存新数据然后退出并再次启动您的应用程序时,它应该显示预期的结果,但是由于您试图在启动应用程序时获取数据,因此显然会显示以前存储的数据。一个简单的解决方案是不为此用户共享实例,您可以执行以下操作:

//Put the currentUser in User class

class User {
   var userID = ""
   var name = ""
   var email = ""

  class var current: User {
       let user = User()
       let defaults = UserDefaults.standard
       user.email = defaults.string(forKey: email_field) ?? ""  //this can be nil if no data
       user.userID = defaults.string(forKey: userID_field) ?? ""
       user.name = defaults.string(forKey: name_field) ?? ""
       return user
  }

  func synchronize() {

       let userDefaults = UserDefaults.standard
       userDefaults.set(self.userID, forKey: userID_field)
       userDefaults.set(self.name, forKey: name_field)
       userDefaults.set(self.email, forKey: email_field)

       print(userDefaults.string(forKey: name_field), userDefaults.string(forKey: userID_field), userDefaults.string(forKey: email_field))
      print(User.current.name, User.current.userID, User.current.email)
  }

}

现在您可以执行操作了。像这样

let newUser = User()
newUser.userID = "123"
....

newUser.synchronize() //save 


//When you need current user 

 User.current