Reporter
allows you to broadcast changes to any number of subscribers.
Think of Reporter
as multidelegate implementation.
Reporter
is particularly useful when implementing Controllers/Services or
other entities with dynamic data changed over time.
- Report (broadcast) to any number of subscribers in the order of subscription
- React to a report (broadcast) with a Swift closure
- Control subscription lifetime with
ReporterBag
- Xcode 9.0+
- Swift 4.0+
Simply add Reporter.swift
(~200 lines of code) file to your Xcode project.
Suppose you have ProfileController
entity that provides the following user
information (incapsulated into ProfileItem
structure):
- user name
- user image
ProfileController
keeps ProfileItem
in one of the following three discrete states:
- no user information is available
ProfileItem
is nil
- user information is available, however, user image has to be loaded
ProfileItem
existsProfileitem.username
existsProfileItem.image
is nil
- user information is available and image has been loaded
ProfileItem
exists- both
ProfileItem.username
andProfileItem.image
exist
As you can see, ProfileController
's ProfileItem
is dynamic and changes
over time. Reporter
helps you subscribe to these changes and keep your
view with the latest data available.
Here's how the example syncs ProfileVC
display with ProfileController
data:
self.profileController.itemChanged.subscribe { [weak self] in
guard
let this = self,
let item = this.profileController.item
else
{
return
}
this.profileVC.item = item
}
Now it doesn't matter who/what/when initiates ProfileItem
updates. ProfileVC
is always in sync with the data.
Sample application (in example/
directory) demonstrates a case of user profile loading.
Here's a brief overview of classes under example/App/Profile
(in the order of importance):
ProfileCoordinator
- creates
ProfileVC
- creates
ProfileController
- requests
ProfileController
data - syncs
ProfileVC
withProfileController
data
- creates
ProfileController
- provides
ProfileItem
anditemChanged
(Reporter
instance) to report (broadcast)ProfileItem
changes - provides execution state of
load()
withisLoading
(Reporter
instance) - simulates
ProfileItem
loading- first, user name
- second, user image
- does so after a short delay
- image update leads to
ProfileItem
update
- provides
ProfileItem
- contains user name
- contains user image
ProfileVC
- displays all three discrete stages of
ProfileItem
availability:- not available
- only user name is available
- both user name and image are available
- displays all three discrete stages of
Loaded image represents Hanar race from Mass Effect.
Reporter is released under the ZLIB license.