Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polling #32

Open
dyedgreen opened this issue Jan 29, 2021 · 3 comments
Open

Polling #32

dyedgreen opened this issue Jan 29, 2021 · 3 comments

Comments

@dyedgreen
Copy link

I was looking at this, and was wondering if there currently is a way to poll a query every N seconds? (E.g. if I display a set of comments, I might want to refresh the view every few seconds such that people are always up-to-date.)

@nerdsupremacist
Copy link
Owner

nerdsupremacist commented Jan 30, 2021

Yes! There is a way to refresh the content. However, I haven't built in any functionality like refreshing every N seconds.

You can get a QueryRefreshController from the Environment. This object lets you ask for a refresh. So your case could be covered by something like this (warning! I haven't tested this implementation. I just guess that it will work)

struct ContentView: View {
    @Environment(\.queryRefreshController)
    var refreshController

    @State
    private var timer: Timer?

    var body: some View {
        VStack {
            ....
        }
        .onAppear {
            DispatchQueue.main.async {
                self.timer = Timer.scheduledTimer(withTimeInterval: 20, repeats: true) { _ in
                    self.refreshController?.refresh()
                }
            }
        }
        .onDisappear {
            self.timer?.invalidate()
        }
    }

@nerdsupremacist
Copy link
Owner

But I would be into the idea of adding a kind of RefreshPolicy modifier to views that are rendering data from GraphQL. Such a RefreshPolicy could decide in which scenarios to refresh (are there any errors? has a mutation occurred?) and to schedule future refreshes.

let api = MyAPI()
let content = api
    .content()
    .refreshPolicy(Polling(every: .seconds(20))

@dyedgreen
Copy link
Author

Yes, that’s sounds really useful! Especially refreshing on mutations of a given field is super good, since that means I can subscribe to updates whenever they happen anywhere in the app 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants