Deprecated UserDefaults (synchronize) method

Be careful

In the process of writing a large number of data to kill the program, the data will be lost, but this should not be written with UserDefaults.

Preface

When a Swift don’t see the story of students in table I used this position to write UserDefaults, the following comments have said it is not necessary to call synchronize (), but before we used the way is to set up a Value synchronize (), to ensure that the data is preserved. Then a small Demo was written to verify the problem.

Xcode and Swift Version

  • Xcode Version 8.2.1
  • Swift 3

Demo

I set up two UIButton and one UILabel.

@IBOutlet weak var tipsLabel: UILabel! @IBAction func setAction (_ sender: UIButton) {UserDefaults.standard.set ("LimChihi forKey:", "Name") (fatalError) @IBAction func readAction (_} sender: UIButton) {let info = UserDefaults.standard.value (forKey: Name) as? String tipsLabel.text info = "nil"}??

The first setAction is used to set the value, and then use fatalError () to make App in the settings immediately after the completion of the crash off, to simulate the most extreme situation.

The second readAction is used to read the value and is displayed on the tipsLabel. If it is empty, it is automatically set to “nil””

Result

I conducted two experiments, the first set value, even if the crash out, App is still able to successfully read the set value. Description in the first set, do not use synchronize () is completely no problem.

The second time, I modify the settings of the Value, the first time I read the stored value (no problem), then re store the new Value (and stored immediately after crash off), the last time the read display is a new revision of Value. It is no problem to modify an existing value without using synchronize.

Finally, Apple on the synchronize () document which has such a paragraph

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

Therefore, the direct use of this wasteful performance method.

To update

I was able to do a test because my method might be missing some of the more extreme conditions.

Override func viewWillAppear (_ animated: Bool DispatchQueue.global (.Async)) {{self.test}) (DispatchQueue.global) (.Async (DispatchQueue.global) {self.test} ({self.test}) (.Async) (DispatchQueue.global).Async (DispatchQueue.global) {self.test} ({self.test}) (.Async) (DispatchQueue.global).Async (DispatchQueue.global) {self.test} (.Async) self.test (DispatchQueue.global)} {(.Async)}} {(self.test) @IBAction func setAction (_ sender: UIButton) {UserDefaults.standard.set ("LimChihi forKey:", "Name") (fatalError) @IBAction func readAction (sender: _} UIButton {let = UserDefaults.standard.value (STR) forKey: "Name" print (STR)?? "nil") (func test) {while} true {var = a 99999999999 * 50000 a = 33}}

With a simple while dead loop to simulate the situation of high CPU load, opened the 8 background threads, and then the maximum CPU occupancy rate of around 720-780%, the results are still set up, set immediately after the crash, does not affect the data.

This experiment can be completely copied.

Deprecated UserDefaults (synchronize) method
screen snapshot 2017-04-06 PM 12.29.55.png

If you have any more extreme conditions, you can also remind me in the comments, I will re test.