简体   繁体   中英

Where should I store the initial data for my Xcode Swift app?

I'm working on a flash card-type app. The data is an array of dictionaries, which must be present when the app is launched, even for the first time.

However, over time, the data in the app will need to be updated - either existing records will be modified or new records will be added. I have an external python script which generates a JSON file and uploads it to my web server. My app needs that file.

Each time the app is launched, my idea is to check the web server. If the data there is newer, then I'll download it, save it and use it. If not (or the web server can't be reached), I'll use the data already in the app, which could be the initial data or newer data that was downloaded and saved previously.

So my questions are:

Where do I store the initial data and where do I store the data that I will eventually download? In order to avoid wasting space, I'd rather not include a JSON file that gets read once and only once, then saved to UserDefaults and then never referenced again.

Is there a way to launch an app with pre-existing UserDefaults? (Seems unlikely.) If I include a JSON file in my app, can I just replace the contents of the file with the new one I downloaded? Or do I need to go with Core Data?

Is there a way to launch an app with pre-existing UserDefaults? (Seems unlikely.)

Actually there is such a way (it's called registering defaults), but that doesn't matter. The JSON that is the basis of your dictionary is not a user default and would never be stored in UserDefaults, so erase that from your mind entirely. UserDefaults is not relevant to this story.

Rather, you would include the initial version of the JSON in the app bundle, as you suggested, and copy it out to, say, the Documents directory on launch if there is not this or a later version already present. (You can use a naming scheme to keep track of versioning.) Subsequently, if you discover a newer version on the web, you just overwrite or remove the existing file from Documents and save the newer file.

All of that presumes that the dictionary is small enough that you can load it all at once into memory, as that is what you will be doing when you read the JSON file. If you were concerned that you could not scale in this way, and that you will need the ability to load just part of your data at a time, that would be a reason to use SQLite or (shudder) Core Data instead, as these approaches allow you to query the database on disk without loading the whole thing into memory at once. Let's say you used SQLite. Then you would include the initial version of the SQLite file in your app bundle and save it out to Documents, or else you would include a JSON version and build the initial SQLite file into Documents on first launch; subsequently, you would use SQL commands to modify the SQLite file in Documents when you discover revised data online.

Adding a json file in your project that is only read once is not a bad idea since json file would be a simple text file that wont occupy any significant amount of space.

But since you want to modify that json file later a better approach would be to first copy that json file from your application bundle to your any available sandbox folders when your app starts (this will allow you to modify the file later).

Later you can check and modify the file if data is available from the webserver. And read the contents of the file later.

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