简体   繁体   中英

Recommended way to store Configuration in a SwiftUI MacOS App

I have to persist configuration in a SwiftUI MacOS App.
Config is around 4KB in size and is a struct containing other structs and arrays.

What would you recommend?

  1. store a hidden (.appname) json-file in in the user's home Directory.
    1a) store a json-file in another location? Which other location is recommended?
  2. use the new @AppStorage mechanism with some enhancements like described in: SwiftUI: What is @AppStorage property wrapper to be able to store structs and arrays. Is 4K to big für User.defaults?

Or is another way better?

Using @AppStorage is the same as using user defaults with more convenience around reads/writes and cascading changes through scenes. According to Apple's documentation there is only a limit on tvOS which is 1MB. 4K should be fine for configuration information on any device.

https://developer.apple.com/documentation/foundation/userdefaults/1617187-sizelimitexceedednotification

Asperi is right that this is an simply an opinion, and up to you.

Here are some things I've noticed about persistance in macOS.

UserDefaults (which @AppStorage uses under the hood) allows you to store simple types, and Data (which could theoretically represent any type). UserDefaults are automatically written to a property list residing under:

~/Library/Preferences/<yourbundleid>.plist

If your app is sandboxed it'll be under:

~/Library/Containers/<Your Container>/Library/Preferences/<yourbundleid>.plist

If you are using a custom method of saving your config/preferences, most developers store such data inside: ~/Library/Application Support/Your App/

I would encourage you to not store your config inside their home directory, unless for some particular reason you absolutely need it there. It can make your app harder to uninstall, and it clutters their home folder!

UserDefaults vs Custom Config?

Once again, just my opinion, but here is the rule I generally follow:

Which is easier?

I usually find myself using UserDefaults for in-context preferences, like the typical " don't bother me about this again " checkbox on a dialog. I also use it often for remembering state (which becomes really awesome with @AppStorage ). As a side note, UserDefaults automatically reads and writes the preferences to disk which in turn means less code.

I do find myself occasionally using custom configs when the preferences are managed outside of the view/vc code, or when my config structure is rather complex. If I'm going to have to go through the work of deserializing/reserializing structs to Data for storage in UserDefaults, I might as well store them and manage them myself.

Size concerns

I've always heard that it's "bad" to store large amounts of preferences inside UserDefaults. While UserDefaults is certainly capable of storing the 4KB you mentioned in your question, there are some situations where you'll want to use alternatives. I found the following SO thread rather enlightening on this subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM