重新启动应用程序后,Swift UserDefault不坚持

发布于 2025-01-31 13:30:11 字数 1645 浏览 3 评论 0原文

当我的应用启动时,我的用户默认设置不会持续存在问题。我已经阅读了有关此事的其他帖子,并且大多数帖子在各种技巧后都得到了标记。我已经实施了所有建议的技巧(我知道),但我仍然有这个问题。

我创建了最简单的示例应用程序。该应用具有一个按钮,该按钮可根据名为“ IsloggedIn”的用户默认设置的当前状态在登录/注销之间切换。

这是代码...

import UIKit

class LoginViewController: UIViewController {

    @IBOutlet weak var loginButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()        
    }

    override func viewDidAppear(_ animated: Bool) {
        refreshButton()
    }

    @IBAction func loginButtonPressed(_ sender: UIButton) {
        let isLoggedIn = UserDefaults.standard.bool(forKey: "isLoggedIn")
    
        UserDefaults.standard.set(!isLoggedIn, forKey: "isLoggedIn")
    
        refreshButton()
    }

    func refreshButton() {
        let isLoggedIn = UserDefaults.standard.bool(forKey: "isLoggedIn")
            
        loginButton.setTitle(isLoggedIn ? "Logout" : "Login", for: .normal)
    }

}

单击按钮时,更新了UserDefault设置,并刷新按钮以根据新设置显示登录或注销。如果我在模拟器中运行该应用程序,我可以看到按钮切换,告诉我UserDefault设置正在正确存储。

当我重新启动应用程序时,就会发生问题。刷新该按钮以显示应用程序关闭时用户默认设置的最后状态。但这并不总是正确地反映先前的状态。有时会这样做,但我经常没有。我也看不到任何模式。

我已经尝试过...

  • 使用set方法,而不是setValue方法
  • 在应用更新后调用synchronize方法(我知道苹果说这是不再需要或建议的)

我只是无法指出我所忽略的东西。有人对我做错什么有任何想法吗?请让我知道我是否可以提供任何可能有帮助的其他代码。

谢谢, Joel

更新

我决定跟踪PLIST文件本身中设置的实际更改。经检查后,我注意到设置需要几秒钟才能在文件中进行物理更新。因此,如果我等待几秒钟,然后关闭应用程序,则设置将节省,并且在我再次启动时将正确显示。如此新的问题是...

  • 为什么我的设置要花这么长时间才能保存?
  • 我可以在正确保存设置之前确保应用程序不会关闭吗?

这很有趣,因为我认为这是Apple所说的不再使用的同步方法的目的。

再次感谢!

I am having a problem with my UserDefault settings not persisting when my app launches. I have read other posts on this matter and most are marked solved after various tips. I have implemented all of the suggested tips (that I am aware of) and I still have this problem.

I have created the simplest sample app. The app has has a button that toggles between Login/Logout depending on the current state of a UserDefault setting called "isLoggedIn."

Here's the code...

import UIKit

class LoginViewController: UIViewController {

    @IBOutlet weak var loginButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()        
    }

    override func viewDidAppear(_ animated: Bool) {
        refreshButton()
    }

    @IBAction func loginButtonPressed(_ sender: UIButton) {
        let isLoggedIn = UserDefaults.standard.bool(forKey: "isLoggedIn")
    
        UserDefaults.standard.set(!isLoggedIn, forKey: "isLoggedIn")
    
        refreshButton()
    }

    func refreshButton() {
        let isLoggedIn = UserDefaults.standard.bool(forKey: "isLoggedIn")
            
        loginButton.setTitle(isLoggedIn ? "Logout" : "Login", for: .normal)
    }

}

When the button is clicked, the UserDefault setting is updated and the button is refreshed to display Login or Logout based on the new setting. If I run the app in the simulator I can see the button toggle which tells me that the UserDefault setting is being stored properly.

The problem occurs when I relaunch the app. The button is refreshed to show the last state of the UserDefault setting when the app was closed. But it doesn't always properly reflect the previous state. Occasionally it does but I more often it does not. I cannot see any pattern here either.

I have tried...

  • Using the set method instead of the setValue method
  • Calling the synchronize method after applying the update (I'm aware that Apple says this is no longer required or suggested)

I just cannot pinpoint what I am overlooking. Does anyone have any ideas about what I am doing wrong? Please let me know if I can provide any additional code that might help.

Thanks,
Joel

UPDATE

I decided to track the actual changes to the setting in the plist file itself. Upon inspection, I noticed that the setting was taking several seconds to physically update in the file. Therefore, if I wait for several seconds before closing my app then the setting will save and it will display properly when I launch again. So new question is...

  • Why does it take so long for my setting to save?
  • and can I ensure that the app does not close before the setting is properly saved?

It's funny because I thought that was the purpose of the synchronize method that Apple say not to use anymore.

Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2025-02-07 13:30:12
import UIKit

class LoginViewController: UIViewController {
    var isLogin = false
    @IBOutlet weak var loginButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        refreshButton()
    }


    @IBAction func loginButtonPressed(_ sender: UIButton) {
        isLogin.toggle()
        UserDefaults.standard.set(isLogin, forKey: "isLoggedIn")
        refreshButton()
    }

    func refreshButton() {
        isLogin = UserDefaults.standard.bool(forKey: "isLoggedIn")
            
        loginButton.setTitle(isLogin ? "Logout" : "Login", for: .normal)
    }

}
import UIKit

class LoginViewController: UIViewController {
    var isLogin = false
    @IBOutlet weak var loginButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        refreshButton()
    }


    @IBAction func loginButtonPressed(_ sender: UIButton) {
        isLogin.toggle()
        UserDefaults.standard.set(isLogin, forKey: "isLoggedIn")
        refreshButton()
    }

    func refreshButton() {
        isLogin = UserDefaults.standard.bool(forKey: "isLoggedIn")
            
        loginButton.setTitle(isLogin ? "Logout" : "Login", for: .normal)
    }

}
空名 2025-02-07 13:30:12

您的问题有一个解释,这是因为userDefault系统的工作方式。通常,保存您的数据需要时间。
在应用程序开发过程中,您可能会丢失数据。
例如,如果您正在使用swift开发应用程序,则当您更改项目中的某些代码并启动应用程序时,请单击xcode中的停止按钮,您可能会丢失其中一些数据,因为那一刻,一些数据仍在内存中等待保存。

但是不用担心,当您的应用程序发布给用户时,它最终将保存。

顺便说一句,我已经测试了项目,我在iPhone上安装了iOS应用,而无需使用xcode,它保存了所有数据,我要么在任务列表(Swipe Out)或单击“主页”按钮,然后再次打开应用程序,数据仍在那里。

在这里,希望这篇文章能清除您的困惑:为什么您会丢失存储在用户默认值中的数据

There is an explanation for your questions, it is because of the way how UserDefault system works. Usually, it takes time to save your data.
You might be losing data during your application development.
For example, if you are developing an application with swift, when you changed some codes in your project and launch your app then click the stop button in Xcode, you will probably lose some of the data, because at that moment, some of the data still in the memory waiting for being saved.

But do not worry, it will be saved eventually when your application is published to the users.

By the way, I have tested my project, I install my iOS app on my iPhone without using XCode, and it saves all of the data either I close my app on the tasks list(swipe out) or click the home button and open the app again, the data still there.

Here, hope this article will clear your confusion: Why Do You Lose Data Stored in User Defaults

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文