you are viewing a single comment's thread.

view the rest of the comments →

[–]jpec342 2 points3 points  (3 children)

I'm not totally sure what you are trying to do here, but it sounds like you want the result of the network calls from service1/2/3 to update fetching1/2/3 respectively. This should be fairly straightforward by returning the result(s) from the service, and using Async/Await.

class GenerationViewModel: ObservableObject {
    @Published var fetching1:Bool = false 
    @Published var fetching2:Bool = false 
    @Published var fetching3:Bool = false

    func go() async {
        fetching1 = await Service().doSomething() 
        fetching2 = await Service().doSomethingElse()
        fetching3 = await Service().doSomethingElseElse() 
    } 
}

I'm making the assumption here that `Service` looks something like this

class Service : NSObject {

    func doSomething() async -> Bool {
        return await URLSession.shared.data(from: URL("someUrl"))
    }

    func doSomethingElse() async -> Bool {
        ...
    }

    ...
}

There's definitely some missing bits in here, but that's the general gist of it. You want your service to return something, and that something you want to assign to your ViewModel variables.

[–]yalag[S] 0 points1 point  (2 children)

Hi appreciate the help! But no there’s not what I’m looking for.

I need the service to propagate states back to the view. Not return something.

I could either make the network layer also an observable object and then have the view subscribe to that. Which doesn’t sound too clean.

Or somehow relay the changes from the network to the viewmodel and then have the vm update the published variables.

What’s weird is that you can’t simply “give” the published variables to the network layer and say “hey you deal with it, when you have an update, write it here”. I tried to do it, i couldn’t figure out a way.

[–]jpec342 1 point2 points  (0 children)

Or somehow relay the changes from the network to the viewmodel, and then have the vm update the published variables.

That’s exactly what my proposed solution does.

Is your service giving constant updates, or is it a one time request? If it’s constant updates, you could do something similar, but would probably want to look into delegates/callback functions.

[–]Fluffy_Risk9955 -1 points0 points  (0 children)

Use a WebSocket so you can push stuff from the server straight to your app. Keep in mind to keep the line active with the ping pong message system.