Online data to swift UI

Hello! I have been working on an app for quite some time now, and one of my views is of a bunch of different articles that my brand has written. The articles can be found on their website, and so as of late, I have just been copying and pasting all of the data from each article into a JSON file in my app; However, as the list of articles grow, I need to fetch the data directly from the website and have it integrated with my code, so every time a new article is published, users dont have to update their app. Is there any way someone could help with this? I've been struggling for a while now. Thanks!

Answered by darkpaw in 794413022

There's a bunch of ways to do this. Let me explain a few ideas I've got...

If your website is using some sort of Content Management System (CMS), then it likely already provides a JSON file that contains this information. You could download that file, interrogate the various parts of it and determine where the important information is held for what your app needs, then load the thumbnail images, headlines and preview text from there.

If it doesn't use a CMS, someone somewhere (you) is always going to have to create this file of metadata. If I were you I'd have it as part of the website/hosting, and your app should download that file. I wouldn't put it in the app itself because you'd have to update the app - which is labour-intensive, and pointless. (We don't have to download a new copy of Apple's News app every time there's a new story.)

What you would do with the JSON file downloaded from the website is take the relevant bits from it, then add the image, headline, preview text to an array of articles in the app, and display those in your view. I'd say sort it by article date descending, and limit how many you put in the view. When a user taps one of the articles, you would then download the full article from the website. It wouldn't make any sense to download the full article as part of the JSON file as the user might not view it.

Be sure to implement error handling if you can't reach the website to download the data. And don't download data you already have, i.e. only download article data for articles after the most recent date in your list of articles.

For example, if you have two articles already downloaded in the app, and they're dated last Monday and last Tuesday, if the JSON file now contains an article for today, you would only bother downloading today's article.

If you're going to limit the number of articles shown your view, then your array can be a FIFO stack, i.e. first in first out. If you want to retain 20 articles then you would remove the oldest article from the array and add a new one into it. Because you're sorting the data in the view anyway there is no need to handling rotating the data in the array, i.e. no need to delete item 0 and move item 1 into slot 0, 2 into slot 1 etc.

Hope this helps.

There's a bunch of ways to do this. Let me explain a few ideas I've got...

If your website is using some sort of Content Management System (CMS), then it likely already provides a JSON file that contains this information. You could download that file, interrogate the various parts of it and determine where the important information is held for what your app needs, then load the thumbnail images, headlines and preview text from there.

If it doesn't use a CMS, someone somewhere (you) is always going to have to create this file of metadata. If I were you I'd have it as part of the website/hosting, and your app should download that file. I wouldn't put it in the app itself because you'd have to update the app - which is labour-intensive, and pointless. (We don't have to download a new copy of Apple's News app every time there's a new story.)

What you would do with the JSON file downloaded from the website is take the relevant bits from it, then add the image, headline, preview text to an array of articles in the app, and display those in your view. I'd say sort it by article date descending, and limit how many you put in the view. When a user taps one of the articles, you would then download the full article from the website. It wouldn't make any sense to download the full article as part of the JSON file as the user might not view it.

Be sure to implement error handling if you can't reach the website to download the data. And don't download data you already have, i.e. only download article data for articles after the most recent date in your list of articles.

For example, if you have two articles already downloaded in the app, and they're dated last Monday and last Tuesday, if the JSON file now contains an article for today, you would only bother downloading today's article.

If you're going to limit the number of articles shown your view, then your array can be a FIFO stack, i.e. first in first out. If you want to retain 20 articles then you would remove the oldest article from the array and add a new one into it. Because you're sorting the data in the view anyway there is no need to handling rotating the data in the array, i.e. no need to delete item 0 and move item 1 into slot 0, 2 into slot 1 etc.

Hope this helps.

To add to @darkpaw points, Fetching website data into memory article contains literature on how to receive data directly into memory from a remote server. This is assuming you have an API that allows you fetch your data.

Online data to swift UI
 
 
Q