AsyncView icon indicating copy to clipboard operation
AsyncView copied to clipboard

AsyncView http request loops inside a TabView

Open chitgoks opened this issue 3 years ago • 4 comments

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

chitgoks avatar Nov 12 '22 13:11 chitgoks

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

  • I fixed by:
    • Remove task and refreshable
    • Handle Task in init function of model
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

ngodacdu avatar Dec 11 '22 16:12 ngodacdu

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.

chitgoks avatar Dec 11 '22 16:12 chitgoks

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

ngodacdu avatar Dec 11 '22 16:12 ngodacdu

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

chitgoks avatar Dec 11 '22 21:12 chitgoks