Manuel Meyer
1 min readNov 22, 2022

--

This article focuses on domain specific modelling and data models (I can't think of a reason why those would be async), but if you look at my other articles, you will find "Khipu", which is based on UseCases. in these UsesCases (or better: in the gateways they are using) you can use any async tool you know in swift — dispatch, OperationQueues, ...

These UseCasses are the same as Robert C. Martin (aka Uncle Bob) uses them in clean architecture.

ie: the light loader use case hands a callback to lightStack.

lights(), just as we are used to it. the stack uses a client as a gateway

'''func lights(_ callback: @escaping (Result<[Light], LightError>) -> ()) {

if let url = URL(string: "http://\(ip)/api/\(user)/lights") {

let task = URLSession(configuration: .default).dataTask(with: URLRequest(url: url)) { data, _, error in

switch error {

case .some(let err): callback(.failure(.lightsEndpoint(err)))

case .none: callback(.success(getLights(from: data!)))

}

}

task.resume()

} else {

callback(.failure(.lightsEndpoint(nil)))

}

}

the use case:

https://gitlab.com/vikingosegundo/brighter-hue/-/blob/master/BrighterHue/Features/Lighting/LightsLoader.swift

the client:

https://gitlab.com/vikingosegundo/brighter-hue/-/blob/master/BrighterHue/Gateways/HueClient.swift

pretty normal stuff.

--

--

Manuel Meyer
Manuel Meyer

Written by Manuel Meyer

Freelance Software Developer and Code Strategist.

Responses (1)