I am using Siesta for REST calls and I am trying to create a simple ResourceObserver to display a SVProgressHUD.
open class SVProgressHUDResourceObserver: ResourceObserver {
static let sharedInstance = SVProgressHUDResourceObserver()
// Show/Hide SVProgressHUD
public func resourceRequestProgress(for resource: Resource, progress: Double) {
print("SVProgressHUDResourceObserver resourceRequestProgress - progress=\(progress)")
if progress == 1 {
print("SVProgressHUD.dismiss()")
SVProgressHUD.dismiss()
} else if !SVProgressHUD.isVisible() {
print("show()")
SVProgressHUD.show()
}
}
public func resourceChanged(_ resource: Resource, event: ResourceEvent) {
print("SVProgressHUDResourceObserver resourceChanged event=\(event)")
}
}
Since it's a singleton I add the observer using a weak reference:
@discardableResult func login(_ username: String, _ password: String,
onSuccess: @escaping (LoginResponse) -> Void,
onFailure: @escaping (Int?, String) -> Void) -> Request {
return service.resource("/app/v1/authentication/login")
.addObserver(SVProgressHUDResourceObserver.sharedInstance)
.request(.post, json: [
"username": username,
"password": password
])
.onSuccess { entity in
guard let loginResponse: LoginResponse = entity.typedContent() else {
onFailure(0, "JSON parsing error")
return
}
self.authToken = loginResponse.session.token
SessionManager.beginNewSession(loginResponse)
onSuccess(loginResponse)
}
.onFailure { (error) in
onFailure(error.httpStatusCode, error.userMessage)
}
}
I purposely added a 10 second delay to the login however the SVProgressHUD never displays, in-fact the resourceRequestProgress method is never called. The authentication process work great.
The log output is:
SVProgressHUDResourceObserver resourceChanged event=observerAdded
SVProgressHUDResourceObserver resourceChanged event=newData(wipe)
Any assistance on getting this to work would be greatly appreciated.