ralfebert / AsyncView

AsyncView is a SwiftUI View for handling in-progress and error states when loading data asynchronously using async/await. It's like AsyncImage but for data.
MIT License
56 stars 9 forks source link

AsyncView http request loops inside a TabView #3

Open chitgoks opened 2 years ago

chitgoks commented 2 years ago

The http request keeps on looping if i place this inside a TabView. What is the workaround for this? Please advise.

ngodacdu commented 1 year ago

The http request keeps on looping if i place this inside a TabView. What is the workaround for this? Please advise.

@chitgoks oh, i'm having the same problem. I think the root cause is adding task (or refreshable) to View. So, render view -> task call -> task finished -> render view -> .... and loops

public var body: some View {
        AsyncResultView(
            result: model.result,
            reloadAction: { Task { await model.load() } },
            content: content
        )
        .task {
            await model.loadIfNeeded()
        }
        .refreshable {
            await model.load()
        }
    }

Ref: async-function-swiftui

public init(asyncOperation: AsyncOperation? = nil) {
        if let asyncOperation {
            asyncOperationBlock = asyncOperation
        }
        Task {
            await load()
        }
    }

p/s: I'm happy if this package gets fixed, it's worth looking forward to

chitgoks commented 1 year ago

I actually got this to work. but seeing as im new to ios, my knowledge is limited.

I placed the AsyncView inside another container, say, a ZStack. Works good.

But i do not understand why.

ngodacdu commented 1 year ago

I also tried as you said but it only solves the infinite loop problem. When i switch to another tab and come back again, api is called again and view will re-render and view's state not kept. Anyway congrats on solving your problem. ✌️✌️✌️

chitgoks commented 1 year ago

also us3d to have that problem. i guess inside your body is only the async view?

what i did was something like

if let obj then show ui else show asyncView block code