RxDataSources
RxDataSources copied to clipboard
IdentifiableType with Unidirectional Data Flow
Based on the documentation: IdentifiableType: The identity provided by the IdentifiableType protocol must be an immutable identifier representing an instance of the model. For example, in case of a Car model, you might want to use the car's plateNumber as its identity.
I am getting a bit confused on how to provide an immutable identifier in the case of a Unidirectional Data Flow.
Take as an example a Restaurant screen with some menu items:
struct RestaurantViewModel: Equatable {
enum State: Equatable {
case failed(Error)
case initialized
case loaded(Restaurant)
case loading
}
enum ViewModelType: IdentifiableType {
case failure(ErrorViewModel)
case hero(RestaurantHeroViewModel)
case item(RestaurantItemViewModel)
var identity: String {
return ????
}
}
let state: State
let viewModels: [ViewModelType]
init(with state: State) {
self.state = state
switch state {
// build array of ViewModelType depending on the state
}
}
}
struct ErrorViewModel: Equatable {
let description: String
}
struct RestaurantHeroViewModel: Equatable {
let name: LoadableTextViewModel
}
struct LoadableTextViewModel: Equatable {
enum State: Equatable {
case initialized
case loaded(NSAttributedString?)
case loading
}
let state: State
let text: NSAttributedString?
init(state: State) {
self.state = state
switch state {
// set text depending on the state
}
}
}
With this approach in mind, it is easy to make all the ViewModels conform to Equatable, but how can you give an immutable identifier to those as they are immutable ViewModels renewed at each state change?
I am a bit confused here as the example projects only handle a basic case with unique numbers on the model, whereas in my case there is no model involved except when the Restaurant is fetched successfully.
Thank you.
I created a simple repo to give something to play with and reproduce what I tried to explain:
https://github.com/florianldt/RxDataSourcesIdentifiableType
The specific line regarding the enum case IdentifiableType: https://github.com/florianldt/RxDataSourcesIdentifiableType/blob/b4059bf7fe76aa62f07b9d1c21ee2ccbffce3deb/RxDataSourcesIdentifiableType/ViewModel.swift#L24
A couple ways to solve this depending on your particular backend/implementation and way that you generate unique IDs.
If you only intend to support to have one model in the loading state at a time, use a static identifier that will never be returned from the backend, like -1 in the cast that IDs are unsigned integers.
An approach that works with multiple models in the loading state is to generate a unique identifier on the client side using something like UUID and this becomes your IdentifiableType. This will have to be stored on your backend so that you can compare it to your existing models.