How to run one-time setup code before executing any XCTest

How to run one-time setup code before executing any XCTest

How to run one-time setup code before executing any XCTest

One approach

When you try to use dispatch_once anyway, Xcode (>=8) is as always very smart and suggests that you should use lazily initialized globals instead. Of course, the term global tends to have everyone indulge in fear and panic, but you can of course limit their scope by making them private/fileprivate (which does the same for file-level declarations), so you don't pollute your namespace.

Imho, they are actually a pretty nice pattern (still, the dose makes the poison...) that can look like this, for example:

private let _doSomethingOneTimeThatDoesNotReturnAResult: Void = {
    print("This will be done one time. It doesn't return a result.")
}()

private let _doSomethingOneTimeThatDoesReturnAResult: String = {
    print("This will be done one time. It returns a result.")
    return "result"
}()

for i in 0...5 {
    print(i)
    _doSomethingOneTimeThatDoesNotReturnAResult
    print(_doSomethingOneTimeThatDoesReturnAResult)
}

This prints:

    This will be done one time. It doesn't return a result.
    This will be done one time. It returns a result.
    0
    result
    1
    result
    2
    result
    3
    result
    4
    result
    5
    result


Side note: Interestingly enough, the private lets are evaluated before the loop even starts, which you can see because if it were not the case, the 0 would have been the very first print. When you comment the loop out, it will still print the first two lines (i.e. evaluate the lets).
However, I guess that this is playground specific behaviour because as stated here and here, globals are normally initialized the first time they are referenced somewhere, thus they shouldn't be evaluated when you comment out the loop.


Model View View-Model (MVVM): Getting Started

Android MVVM 

MVVM stands for ModelViewViewModel.

Model: This app contains data. He cannot speak directly to the scenario. In general, it is recommended to expose the data to the viewmodel by the observables.

View: It represents the UI of any application logic-free application. It observes the viewmodel.

ViewModel: It acts as a link between the model and the view. It is responsible for transforming the data from the model. It provides the data stream to the view. It also uses hooks or callback to update the view. It wants data from the model.

The following flow describes the main MVVM pattern.