- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
因此,我查看了有关 NSUserDefaults 的 synchronize() 方法的苹果文档。引用如下:
https://developer.apple.com/reference/foundation/userdefaults/1414005-synchronize
当前页面显示:
Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.
但是,我还是不明白的是这个方法到底应该在什么时候调用呢?例如,是否应在每次用户更改应用程序设置时调用它?或者我应该相信后台 api 会处理这个问题吗?在内存中更改设置后立即离开 View 是否会导致更改丢失?
另外,什么时候调用 synchronize() 失败会导致用户设置无法正确更改?
此外,调用此方法的成本(性能、内存或其他)是多少?我知道这涉及到从磁盘读取和写入磁盘,但在手机上真的需要那么多精力吗?
最佳答案
关于用户默认值似乎有太多的混淆。这样想。它本质上与您在整个应用程序中使用全局词典是一样的。如果您向全局字典添加/编辑/删除键/值,则该更改会立即在您的代码中的任何位置可见。由于该字典在内存中,如果它没有保存到文件中,则当您的应用程序终止时,所有内容都会丢失。 NSUserDefaults
每隔一段时间自动将字典保存到文件中。
synchronize
方法的唯一原因是您的应用程序可以告诉 NSUserDefaults
“现在”保留字典,而不是等待最终会发生的自动保存.
您需要这样做的唯一原因是您的应用可能会在下一次自动保存之前终止(或崩溃)。
在我自己的应用程序中,我调用 synchronize
的唯一地方是在 applicationDidEnterBackground
委托(delegate)方法中。这是为了确保在应用程序在后台终止时保留最新的未保存更改。
我认为很多困惑来自于在开发过程中调试应用程序。在开发过程中,使用调试器中的“停止”按钮终止应用程序的情况并不少见。很多时候这种情况发生在最近的 NSUserDefaults
更改被持久化之前。因此,我养成了在调试器中终止应用程序之前按下 Home 按钮将我的应用程序置于后台的习惯,以确保保留最新更新。
鉴于以上总结,让我们回顾一下您的问题:
should it be called every time the user changes the app's settings?
没有。如上所述,任何更改都会立即自动可用。
Or should I just trust that the background api is going to handle that?
是的,除了在您的应用程序进入后台时调用 synchronize
之外,请相信自动持久化。
And does the leaving of the view immediately after a settings change in memory result in that change being lost?
这没有效果。在 NSUserDefaults
中添加/编辑/删除键/值后,就会进行更改。
Also, when might a failure to call synchronize() result in user settings not getting changed correctly?
更改唯一可能丢失的情况是,如果您的应用程序在最新更改被保留之前终止。当您的应用程序进入后台时调用 synchronize
可以解决大部分问题。唯一剩下的可能问题是您的应用程序是否崩溃。任何尚未保存的未保存更改都将丢失。修复您的应用,使其不会崩溃。
Furthermore, what is the cost (performance, memory or otherwise) of calling this method? I know it involves reading and writing from/to the disk but does that really take that much effort on phones?
自动持久化在后台完成,它只是将字典写入 plist 文件。除非您不遵循建议,否则它非常快。如果您滥用 NSUserDefaults
来存储大量数据,它会变慢。
关于ios - 何时以及为什么应该使用 NSUserDefaults 的 synchronize() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808072/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!