wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
266 stars 109 forks source link

Show network activity indicator while loading #71

Closed davidgoli closed 8 years ago

davidgoli commented 8 years ago

This is a nice-to-have. Currently we can explicitly call UIApplication.sharedApplication().networkActivityIndicatorVisible = true before/after each request, but that becomes cumbersome and clutters the codebase with boilerplate. The preferred option is to just provide a boolean to toggle this behavior on/off globally for the Spine instance or the HTTPClient instance. Another option is to provide a hook in HTTPClient for request began/ended so that we could implement this ourselves and/or possibly other behaviors around network activity.

wvteijlingen commented 8 years ago

Nice idea. I think the best way to implement such a thing would be to create hooks, as you mentioned. This allows you to add any kind of functionality that depends on network activity. Perhaps we can add a delegate to the standard HTTPClient. Custom NetworkClients can implement delegates or hooks themselves.

wvteijlingen commented 8 years ago

What do you think about the following:

public protocol NetworkClientDelegate {
    func networkClient(client: NetworkClient, willPerformRequestWithMethod: String, URL: NSURL, payload: NSData?)
    func networkClient(client: NetworkClient, didPerformRequestWithMethod: String, URL: NSURL, success: Bool)
}
wvteijlingen commented 8 years ago

I've added a HTTPClientDelegate protocol to the built in HTTPClient. You can use this to implement such functionality. Custom NetworkClients can incorporate their own delegate protocol.

davidgoli commented 8 years ago

Sounds great, thank you!