UserDefaults is not saved with Swift 3

2019-06-16 05:37发布

I'm trying to use the UserDefaults to persistently save a boolean value. This is my code :

public static var isOffline = UserDefaults.standard.bool(forKey: "isOffline") {
        didSet {
            print("Saving isOffline flag which is now \(isOffline)")
            UserDefaults.standard.set(isOffline, forKey: "isOffline")
            UserDefaults.standard.synchronize()
        }
    }

Why doesn't work? What is the problem in this code?

EDIT: The problem is that when I try to retrieve "isOffline" key from UserDefaults I always get a false.

EDIT 2: I set the isOffline in the .onChange method of the row (I'm using Eureka as framework to create forms). The flag keep the right value during the lifecycle of the app, but when I close it that value is probably removed in some way.

3条回答
2楼-- · 2019-06-16 05:54

try to change

UserDefaults.standard.set(isOffline, forKey: "isOffline")

to

UserDefaults.standard.setValue(isOffline, forKey: "isOffline")

without the dispatch code

查看更多
来,给爷笑一个
3楼-- · 2019-06-16 05:57

Do Like this,

public static var isOffline:Bool {
    get {
       return UserDefaults.standard.bool(forKey: "isOffline")
    }
    set(newValue) {
        print("Saving isOffline flag which is now \(isOffline)")
        UserDefaults.standard.set(newValue, forKey: "isOffline")
        UserDefaults.standard.synchronize()
    }
}
查看更多
唯我独甜
4楼-- · 2019-06-16 06:01

I had the same problem and the issue was in the "didSet" block itself. I don't know why, but it does not work with userDefaults - it does not persist it properly and after killing the application all changes were gone.

Synchronize() does not help. I found out, this method is no longer necessary and it will be deprecated in future (this is comment in UserDefaults class):

-synchronize is deprecated and will be marked with the NS_DEPRECATED macro in a future release.

By trial and error I found out, that it works, if I call it from main thread:

public static var isOffline = UserDefaults.standard.bool(forKey: "isOffline") {
        didSet {
            print("Saving isOffline flag which is now \(isOffline)")
            DispatchQueue.main.async {
                    UserDefaults.standard.set(isOffline, forKey: "isOffline")
                }

        }
    }

If anyone can explain, why it works on main thread and no other, I would be glad to hear it.

查看更多
登录 后发表回答