Current
is the global instance of theWorld
type which is located in theBabylonDependencies.framework
. Its purpose is to provide convenient access to the instances that should be shared between different parts of the app (cross-cutting concerns). For more information please read the How Control the World proposal.
All the properties in Current
must have their initial values provided at initialisation time.
But that's not the reality for all of them. These async
properties have their initial value provided in the SharedAppDelegate
through the method setupCurrent
. Only after that, the whole world is ready to be used.
- access
Current
only from topmostAppDelegate
untilBuilder
s layer - pass the required dependencies down the chain;
- create a customized version to fit your specific needs for tests and set it in the test
setUp
method. Please reset it on thetearDown
method
override func setUp() {
let utc = TimeZone(identifier: "UTC")!
let locale = Locale(identifier: "en_GB")
Current = World(autoupdatingTimeZone: utc, autoupdatingLocale: locale)
}
override func tearDown() {
Current = .production // which returns a new `World()`
}
- access
Current
from any other layer other thanBuilder
. All the other layers (ViewModel
,FlowController
,ViewController
,Model
,BusinessController
) continue to receive their dependencies through injection when created by the builder, preferably at initialisation time; - inject
current
as a dependency; - mutate
Current
's properties – or re-affect it to another instance – afterSharedAppDelegate.setupCurrent()
. You are only allowed to mutateCurrent
in tests, not in app. InRelease
configuration it's a constantlet
anyway to prevent mutation;
#if DEBUG
public var Current: World = .production
#else
public let Current: World = .production
#endif
Proposal reference: How Control the World