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

Question / Idea: Track changes in the request's status #99

Open
richardtop opened this issue Mar 21, 2025 · 0 comments
Open

Question / Idea: Track changes in the request's status #99

richardtop opened this issue Mar 21, 2025 · 0 comments

Comments

@richardtop
Copy link

richardtop commented Mar 21, 2025

Consider the following, when this library is used together with CreateAPI: https://github.com/CreateAPI/CreateAPI

    func myInfo() async throws -> [Paths.Info.My.GetResponseItem] {
        let request = Paths.info.my.get
        let response = try await api.send(request)
        return response.value
    }

Normally, the UI will have some spinner or a loading indicator to show the fact that the request is being sent and the UI is being updated. This is even more important in case of non-GET requests, such as POST which might modify the resource. The UI should be blocked while the operation is ongoing.

One way to accomplish this is to use a defer construct:

func getNewInfo() {
    defer { isInProgress = false }
    isInProgress = true
    client.myInfo()...
    ... // Process the response
}

However, this is cumbersome and error-prone, as it requires attention from the developer to add this logic around every request that's being sent with the client.

I'm asking whether this is currently available or possible to implement / consider as an idea for this project to have some sort of a state publisher set on the request itself, so that an arbitrary number of subscribers can attach to that publisher and connect their UI states directly to the result of that request.

This makes easy implementing the same scenario for multiple requests executed in parallel and enabling the UI only when all of them have been completed.

If this is actually possible right now, please let me know how I can implement this "request tracking" design in my code.

From a technical standpoint, this should be possible by leveraging the URLSessionTask delegate and other properties. Connecting to those properties could allow to expose the progress and the status of the request via Get's API:

open class URLSessionTask : NSObject, NSCopying, ProgressReporting, @unchecked Sendable {

    @available(iOS 15.0, *)
    open var delegate: (any URLSessionTaskDelegate)?

    @available(iOS 11.0, *)
    open var progress: Progress { get }
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

1 participant