Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdentifiableType with Unidirectional Data Flow #419

Open
florianldt opened this issue Nov 2, 2022 · 2 comments
Open

IdentifiableType with Unidirectional Data Flow #419

florianldt opened this issue Nov 2, 2022 · 2 comments

Comments

@florianldt
Copy link

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.

@florianldt
Copy link
Author

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

@bennnjamin
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants