# Data Flow

<figure><img src="/files/vba8hFkp3R9kVw9Mdf3h" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/6LVFXXEyI0RV4yYC9vng" alt=""><figcaption></figcaption></figure>

Let's get an overview of the basic data structures used in the system and how they are consumed by predefined automated views to streamline data flow and handle common view states.

#### Data Structures

* **`RemoteData<T>`**&#x77;hich is an alias of **`Option<Either<ErrMessage, T>>`:** Represents a single piece of data, including its loading state and potential errors.
* **`RemoteDataPaginated<T>`** an alias of **`Option<Either<ErrMessage, Page>>`**: Represents a paginated list of data, including information about the current page, total pages, and the data itself.
* **`RemoteListData<T>`** alias of **`Option<Either<ErrMessage, List>>`:** Represents a simple list of data.

**Key Components:**

* **`Option`:** A type from ArrowKT that represents an optional value, which can be `Some` (present) or `None` (absent).
* **`Either`:** Another type from ArrowKT that represents a value that can be either a `Right` (success) or a `Left` (error).

**Purpose of `Option` and `Either`:**

* **`Option`:** Used to represent the initial loading state (none) and potential errors (left).
* **`Either`:** Used to represent the success or error outcomes of data fetching.

#### Data Flow

1. **Data Fetching:** The repository fetches data from the server using HTTP requests.
2. **Data Processing:** The repository processes the fetched data, transforming it into one of the three data structures.
3. **Data Propagation:** The data structure is propagated through the view model to the view.
4. **View Rendering:** The view automatically handles the loading state, error display, and data rendering based on the data structure.

#### Usage Examples

**`RemoteData<T>`:**

Kotlin

```
// ViewModel
private val _todoItem = MutableStateFlow<RemoteData<TodoResponse>>(none())
val todoItem = _todoItem.asStateFlow()

// View
StatelessSurface(
    remoteData = todoVM.todoItem.collectAsState().value
) { todoItem ->
    // Your view for that item
}
```

Use code with caution.

**`RemoteDataPaginated<T>`:**

Kotlin

```
// ViewModel
private val _todos = MutableStateFlow<RemoteDataPaginated<TodoResponse>>(none())
val todos = _todos.asStateFlow()

// View
LazyColumnWithLoading(
    remoteData = todoVM.todos.collectAsState().value
) { todoItem ->
    TodoItemView(todoItem) // Your custom view for single item
}
```

Use code with caution.

**`RemoteListData<T>`:**

Kotlin

```
// ViewModel
private val _todos = MutableStateFlow<RemoteListData<TodoResponse>>(none())
val todos = _todos.asStateFlow()

// View
LazyColumnWithLoadingForList(
    remoteData = todoVM.todos.collectAsState().value
) { todoItem ->
    TodoItemView(todoItem) // Your custom view for single item
}
```

Use code with caution.

#### Benefits of Using These Data Structures

* **Simplified View Logic:** Predefined views automatically handle loading states and errors, reducing boilerplate code.
* **Consistent Error Handling:** The `Either` type provides a consistent way to handle errors.
* **Flexibility:** These data structures can be used to represent various types of data.
* **Improved Readability:** The `Option` type makes the code more expressive and easier to understand.

By understanding these data structures and their usage, you can effectively manage data flow and improve the user experience in your KMM applications.

&#x20;

{% hint style="info" %}
If you have multiple files, GitBook makes it easy to import full repositories too — allowing you to keep your GitBook content in sync.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cognitox.gitbook.io/cognito-kmm-docs/architecture/markdown-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
