Aromal Sasidharan Aromal Sasidharan - 1 year ago 406
iOS Question

Rxswift - how to show a progress bar

I need to show a progress bar upon an API call and hide it after the API call completes. The following is code I have written to populate a table. Where should I make calls to show and hide progress for the API being called? Is there an

way of doing this?

items = fetchAllAnswers()
items.bindTo(self.myTableView.rx_itemsWithCellIdentifier("cellIdentifier", cellType: UITableViewCell.self)){ (row, element, cell) in
cell.textLabel?.text = element

func fetchAllAnswers() -> Observable<[String]>{
let api = Observable.create { (obsever: AnyObserver<[String]>) -> Disposable in
let items = Api.getUsers()

return AnonymousDisposable{
print("api dispose called")
return api

Answer Source

You may to use ActivityIndicator from RxSwift repo. I using MBProgressHUD in my project. At first you need to create extension for this library:

extension MBProgressHUD {

     Bindable sink for MBProgressHUD show/hide methods.
    public var rx_mbprogresshud_animating: AnyObserver<Bool> {
        return AnyObserver { event in

            switch (event) {
            case .Next(let value):
                if value {
                    let loadingNotification = MBProgressHUD.showHUDAddedTo(UIApplication.sharedApplication().keyWindow?.subviews.last, animated: true)
                    loadingNotification.mode = self.mode
                    loadingNotification.labelText = self.labelText
                    loadingNotification.dimBackground = self.dimBackground
                } else {
                    MBProgressHUD.hideHUDForView(UIApplication.sharedApplication().keyWindow?.subviews.last, animated: true)
            case .Error(let error):
                let error = "Binding error to UI: \(error)"
                #if DEBUG
            case .Completed:

Next you need to create ActivityIndicator object in your ViewController class:

let progress = MBProgressHUD()
progress.mode = MBProgressHUDMode.Indeterminate
progress.labelText = "Loading..."
progress.dimBackground = true

let indicator = ActivityIndicator()

Next just use trackActivity() function into your sequences:

.subscribeNext { stringArray in 
    items.value = stringArray
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download