Skip to content

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

@richardtop

Description

@richardtop

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 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions