ResourceObserver

public protocol ResourceObserver

Something that can observe changes to the state of a Resource. “State” means latestData, latestError, and isLoading.

Any code that wants to display or process a resource’s content should register itself as an observer using Resource.addObserver(...).

  • Called when anything happens that might change the value of the reosurce’s latestData, latestError, or isLoading flag. The event explains the reason for the notification.

    Declaration

    Swift

    func resourceChanged(_ resource: Resource, event: ResourceEvent)
  • Receive updates on progress at regular intervals while a request is in progress. Will always receive a call with a value of 1 when the request completes.

    Default Implementation

    Does nothing.

    Declaration

    Swift

    func resourceRequestProgress(for resource: Resource, progress: Double)
  • stoppedObserving(resource:) Default implementation

    Called when this observer stops observing a resource, if the observer itself still exists. Use for making Resource.removeObservers(ownedBy:) trigger other cleanup.

    Warning

    This method is not called for self-owned observers when the observer itself being deallocated is what caused it to stop observing. This is because there is no way for Siesta to know that observer is about to be deallocated; it can only check whether the observer is already gone.

    For example:

    var myObserver: MyObserver? = MyObserver()
    resource.addObserver(myObserver!)  // myObserver is self-owned, so...
    myObserver = nil                   // this deallocates it, but...
    // ...myObserver never receives stoppedObserving(resource:).
    

    In the situation above, MyObserver should implement any end-of-lifcycle cleanup using deinit.

    Default Implementation

    Does nothing.

    Declaration

    Swift

    func stoppedObserving(resource: Resource)
  • observerIdentity Default implementation

    Allows you to prevent redundant observers from being added to the same resource. If an existing observer has an identity equals to a new observer, then Resource.addObserver(...) has no effect.

    Default Implementation

    True iff self and other are (1) both objects and (2) are the same object.

    Declaration

    Swift

    var observerIdentity: AnyHashable { get }