UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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()
    }
}

2      

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 = ""
}

2      

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...

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.