Open florianldt opened 2 years ago
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.
Based on the documentation:
IdentifiableType
: Theidentity
provided by theIdentifiableType
protocol must be an immutable identifier representing an instance of the model. For example, in case of aCar
model, you might want to use the car'splateNumber
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:
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.