Skip to content

Correct way to implement .refreshable with loneliving effects #2542

Answered by mbrandonw
maja0270558 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @maja0270558, this is just vanilla SwiftUI behavior. Here's a reproduction of the problem using just @State:

struct BuggyView: View {
  @State var isLoading = false
  var body: some View {
    ScrollView {
      Text("isLoading: \(self.isLoading.description)")
    }
    .refreshable {
      self.isLoading = true
      defer { self.isLoading = false }
      do {
        try await Task.sleep(for: .seconds(1))
      } catch {
        print(error)
      }
    }
  }
}

#Preview {
  BuggyView()
}

If you run this you will find that doing pull-to-refresh causes the async work to be cancelled immediately. It seems that modifying any state while the async work is being performed causes the async…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@maja0270558
Comment options

@mbrandonw
Comment options

Answer selected by maja0270558
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants