SwiftUI app - Easiest and efficient way to store data: database or json?

I am building a recipe app and i have all my recipes raw data (ingredients, recipes, and instructions) are stored on a singe data.swift file. The problem is the file is about 3000 lines long for 100 recipes. It is easy to access the data because it on the app but is it going to slow down the app with all the data inside the app rather than from a database? Would it be better to put the data in a json file or a database? Also, if i go with a database, i have an image for each recipe, would i need to store the images on the database as well, rather than they be in the app in the assets folder?

You don’t need a database at all.

Since you already have a JSON file configured then obviously you know how to update it if you need too. With that in mind, the simplest way is to host that file somewhere and the best place is Github. There you can change the file whenever you want to update it with new recipes or amended recipes and the App will pick up the changes once you create the necessary code to remotely retrieve that JSON file.

It’s much easier than having the data stored with the App because if you store it as a file in the App then any change to the data will mean that you need to submit a new version of the App with the revised JSON file. Far too messy.

All you need to do is remotely retrieve the JSON file and that’s easy enough to do with a URLSession. Chris Ching has a number of Apps for which there is a course that shows you how to do that.

Check out his learn.codewithchris.com site.

1 Like

Thanks for the reply Chris. I just wanted to clarify that right now, all my data is on the data.swift file in my app (one for recipes, one for categories). I realize that having that much info in one file may slow down my app and make it harder to add new recipes without updating the app. But i was told by someone on StackOverflow that either a database or JSON would be more efficient. Im not overly familiar with JSON but i know a little. I would like to know where i can go to assist me on how to place my data is a json, then how i can use it for my app, like how i can access the data from my app. I have a website where i could host it but don’t know how or Github is fine too. Can you point me to the particular app tutorials Chris Ching has that can help me?
Liz

What is your preference? Storyboard or SwiftUI?

There is a Recipe App that Chris Ching builds using SwiftUI.

It’s in the course titled iOS Foundations (SwiftUI). See the attached screenshot. You will need to subscribe to CWC+ to get access to it which you can do on a month by month basis. You can discontinue your subscription at any time.

If you are new to SwiftUI then I would suggest you start the course at the beginning and work through it so that you get a good grounding in the SwiftUI framework.

The Recipe App is Module 5 and it uses a JSON file (embedded in the App) as the data source for the recipes and the images for the recipes are stored in the assets catalogue. This does not solve your need for a database but at least you are building your JSON skills.

Module 6 is the Learning App which includes retrieving JSON code from a remote location so that will give you the skills in performing Network calls.

1 Like

This sounds perfect. I will give this a try. I am not a NOVICE in SwiftUI and SwiftUI is my preference for any project i do. I have one on the AppStore now called #yolo but you will need to put in this: “Yolo Fitness” in your search. Thank you for your help and once complete, i will let you know if this solves my problem. Have a great day, Chris!

Hi Chris, I finished all the tutorials for the Recipe app but the last question i have is with 100 recipes in a json file, is that too big to have on the device? Will it slow down the app on the device? Right now i have a regular data.swift file with the 100 recipes but if i change it to a data.json format, will this be more efficient or do i need to put all these recipes on the json file in another location rather than in the app itself? Thanks :slight_smile:

Hi Liz,

I guess it really depends on what your overall goal is.
Is the App just for yourself or is it going to be widely available to other users?
Is it going to be free or a paid App?
Lot’s of things to consider.

If it is just for yourself and you want to easily add to the list of recipes then, as I said before, it may be easier to have your data in a JSON file and sitting on a remote server to enable you to update that file when you want to add more recipes to the list. If there are images associated with the recipes then they too can be placed on a remote server and be downloaded for display on the App.

1 Like

Hi Chris,
The app will be available to anyone on AppStore free but will eventually implement InApp Purchases. I want to eventually update or add new recipes without having to update the app each time. Your response to put it in JSON file and Images placed on a remote server sounds good to me, but how do i do that? How do i access the recipes, is it much different that accessing it like it is shown on the Recipe app in CWC+?

Hi Chris! So i was speaking to someone on StackOverflow and they suggested that having the 100 recipes in a json file, stored locally on my app will not slow down the app, is that true? Any idea how i can store it (json file) on my shared-hosted website so i can update when i want without having to update the app each time i want to add a new recipe? Can you suggest any tutorials to show me how?

It probably will not slow down you App. It just a question of if you want to make it easier for yourself to update the data stored on a remote server (website) rather than submit a revised App to the App Store each time you want to update the data.

If you already have your own website and you are managing the content yourself then all you need to do is create a folder on that website to store that file and then reference the path in your App using a URLSession to retrieve it.

The Learning App on CWC+ will give you the skills to achieve that. It is Module 5 in the “iOS Foundations (SwiftUI)” course.

Great! I will try that once i finish changing the data file format to json. Thank you :blush: