API Reference / iOS InstantSearch Widgets / Loading
Apr. 24, 2019

About this widget

Components that show a loading indicator during pending requests.

To add a loading indicator to your search experience, use these components:

  • Searcher: The Searcher that handles your searches.
  • LoadingInteractor: Business logic that handles showing a loading indicator
  • LoadingController: The controller that interfaces with a concrete loading indicator.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let activityIndicator = UIActivityIndicatorView()

let loadingInteractor: LoadingInteractor
let activityIndicatorController: ActivityIndicatorController
let searcher: SingleIndexSearcher

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  searcher = SingleIndexSearcher(...)
  loadingInteractor = .init()
  activityIndicatorController = .init(searchBar: loadingIndicator)

  super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

override func viewDidLoad() {
  super.viewDidLoad()

  loadingInteractor.connectSearcher(searcher)
  loadingInteractor.connectController(activityIndicatorController)

  // Execute a search which will spin the loading indicator until the results arrive
  searcher.search()
}

Customize your view

Controllers provided by default, like the ActivityIndicatorController work well when you want to use native UIKit with their default behavior like UIActivityIndicatorView.

If you want to use another component such as a third-party view, or want to introduce some custom behavior to the already provided UIKit components, you can create your own controller conforming to the LoadingController protocol.

Protocol

func setItem(_ item: Bool)

Function called when a new state of the loading indicator is set.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
public class ActivityIndicatorController: LoadingController {

  let activityIndicator: UIActivityIndicatorView

  public init (activityIndicator: UIActivityIndicatorView) {
    self.activityIndicator = activityIndicator
  }

  public func setItem(_ item: Bool) {
    item ? activityIndicator.startAnimating() : activityIndicator.stopAnimating()
  }

}

Did you find this page helpful?