BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: How do I return data from an async function?

Forums > 100 Days of SwiftUI

@ian  

Hello,

I am on day 52 and trying to make a little app using what I have learned so far. The idea of the app is that it will download a .txt or .md file from GitHub and turn that into a todo list.

I have gotten as far as the below and my code is able to download and print a test file. However I can't quite figure out how to return that data from my download function and place it in my class or a Text field or anything.

How do I get my results into my list object?

Thank you.

import SwiftUI

struct ContentView: View {

    @ObservedObject var list = List()

    var body: some View {
        VStack(alignment: .leading) {
                Text("")
        }
        .task {
            await loadData()
        }
    }

    func loadData() async {

        let url = URL(string: "https://raw.githubusercontent.com/acwright/ImportExport/main/README.md")!

        let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
            if let localURL = localURL {
                if let result = try? String(contentsOf: localURL) {
                    print(result)

                }
            }
        }
        task.resume()

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

   

I change there list to a String as you have not put that there List()

struct ContentView: View {

    @ObservedObject var list = List()

    var body: some View {
        VStack(alignment: .leading) {
            Text(list.text)
        }
        .task {
            await loadData()
        }
    }

    func loadData() async {

        let url = URL(string: "https://raw.githubusercontent.com/acwright/ImportExport/main/README.md")!

        let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
            if let localURL = localURL {
                if let result = try? String(contentsOf: localURL) {
                    DispatchQueue.main.async {
                        list.text = result
                    }

                }
            }
        }
        task.resume()
    }
}
class List: ObservableObject {
    @Published var text = ""
}

   

Hi @ian! Here is the general idea how that can be implemented CHECK HERE though article name states about async task, Paul shows how you can decode your result. But I suppose that downloanding md or txt files will be much more difficult to handle than for example json files. All depends on what you place in your task list as well. With md and text files i guess you will have to create parse function so that to place necessary data into your to do list. That will go through the file and separates that by - ok, this is task one, task two etc...

1      

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.