You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
structRestaurantViewModel:Equatable{enumState:Equatable{case failed(Error)case initialized
case loaded(Restaurant)case loading
}enumViewModelType:IdentifiableType{case failure(ErrorViewModel)case hero(RestaurantHeroViewModel)case item(RestaurantItemViewModel)varidentity:String{return????}}letstate:StateletviewModels:[ViewModelType]init(with state:State){self.state = state
switch state {// build array of ViewModelType depending on the state}}}structErrorViewModel:Equatable{letdescription:String}structRestaurantHeroViewModel:Equatable{letname:LoadableTextViewModel}structLoadableTextViewModel:Equatable{enumState:Equatable{case initialized
case loaded(NSAttributedString?)case loading
}letstate:Statelettext: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.
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: