How do I download data from web and pass it them to TableViewController?

As a newbie in the Swift programming language as well as programming for iOS, I am trying to make a very simple application.

I just want to download the data from a website (parse and get what I want) and finally show it them to the user. Why does this in Swift seem so difficult? Is it or not?

In the end I have managed to download, parse and keep the data I want in a swift class that I designed and is responsible for this process. So, I have my class "DataFromWeb", in which I have implemented various methods to get what I want. This class works.

How can I call it and get the data it holds in the main TableViewController?
I want a simple list on the user's screen to display a title in each cell.
Below is my code :

import Foundation
import SwiftSoup

class Announcements
{
    private var annTitles = [String]()


    // https://stackoverflow.com/questions/25407447/getting-data-from-a-website-in-swift
    // https://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift
    private func getHTML(aURL: String, completion: ((Bool) -> (Void))?)
    {
                let url = URL(string: aURL)!
                let task = URLSession.shared.dataTask(with: url)
                {
                    (data, response, error) in
                    guard let data = data else { return }
        //            print(String(data: data, encoding: .utf8)!)
                    let tempHTML = String(data: data, encoding: .utf8)!

                    completion?(tempHTML)
                }
                task.resume()
    }
        
       

    public func getAnnouncements()
    {
        self.getHTML(aURL: "https://www.my.site/news.php")
        { result in
            if result.isEmpty
            {
                print("Announcements could not be downloaded!")
            }
            else
            {
                print("Successful download of announcements.")
                self.parseHTML(html: result)
            }
            // Default
        }
    }
    
    
    private func parseHTML(html : String)
    {
        do
        {
            let doc: Document = try SwiftSoup.parse( html )
            let table: Elements = try doc.getElementsByClass( "table" )
            
            for tr in try! table.select("tr")[1...100]
            {
                let td : Elements = try tr.select("td")
                self.annTitles.append( try td[0].text() )
            }
//            self.viewData()
        }
        catch Exception.Error(let type, let message)
        {
            print(type)
            print(message)
        }
        catch
        {
            print("SwiftSoup parsing data error!")
        }
        
        print("All good!")
    }
    
    
    public func getTitles() -> [String]
    {
        return self.annTitles
    }

    
    public func viewData()
    {
        for  i in 0...99
        {
            print( self.annTitles[i] )
        }
    }
    
    
}

Because the getHTML method implements an asynchronous process, I can't just call it them, in the TableViewController. Specifically, I do not know when to load the data into the list, as I do not know when they have been downloaded. How do I know when the data has been downloaded in the TableViewController class? Can I show a "spinner" to the user, until the data is downloaded, and if they do, the list is displayed?

How does my above design and implementation look like? Do you like? Do you think it's wrong?
In the above way I want to achieve a more flexible code so that the backed functionality stands out from the GUI.

Thank you. :slight_smile:

Terms of Service

Privacy Policy

Cookie Policy