> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Stores & Data

> Configure what CometChat iOS UI Kit v6 components fetch, read their state, and change their data — the stores, request builders and list operations in CometChatCore.

Each component's **store** lives in `CometChatCore` and owns fetching, paging, real-time SDK
listeners and the Kit's events. Both toolkits use the same store. Stores are `@MainActor` and `@Observable`.

***

## Creating and Providing a Store

By default each component creates its own store. To configure it, create it yourself and pass
it in.

> [tab] SwiftUI
>
> ```swift theme={null}
> struct PeopleScreen: View {
>     @State private var store = UsersStore(
>         usersRequestBuilder: UsersRequest.UsersRequestBuilder(limit: 20).friendsOnly(true)
>     )
>
>     var body: some View {
>         CometChatUsers(store: store)
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let store = UsersStore(
>     usersRequestBuilder: UsersRequest.UsersRequestBuilder(limit: 20).friendsOnly(true)
> )
> let users = CometChatUsers(usersStore: store)
> ```
>
> The store stays reachable afterwards as `users.usersStore`.

| Component | Store | SwiftUI argument | UIKit |
| - | - | - | - |
| Conversations | `ConversationsStore` | `store:` | `init(conversationsStore:)`, `conversationsStore` |
| Users | `UsersStore` | `store:` | `init(usersStore:)`, `usersStore` |
| Groups | `GroupsStore` | `store:` | `groupsStore` |
| Group Members | `GroupMembersStore` | `store:` | `groupMembersStore` |
| Message List | `MessageListStore` | `store:` | `store` |
| Message Composer | `MessageComposerStore` | `store:` | `messageComposerStore` |
| Message Header | `MessageHeaderStore` | `store:` | `store` |
| Search | `SearchStore` | `searchStore:` | `searchStore` |
| Call Logs | `CallLogsStore` | `store:` | `callLogsStore` |
| Call Buttons | `CallButtonsStore` | `store:` | `store` |
| Pinned / Saved Messages | `PinnedMessagesStore`, `SavedMessagesStore` | `store:` | `pinnedMessagesStore`, `viewModel` |

***

## State Observation

A store publishes its screen state as an enum. For Conversations:

```swift theme={null}
func describe(_ state: ConversationsState) -> String {
    switch state {
    case .loading: "Loading"
    case .populated(let rows, let hasMore): "\(rows.count) rows\(hasMore ? ", more to load" : "")"
    case .paginating(let rows): "\(rows.count) rows, loading more"
    case .empty: "No conversations"
    case .searchEmpty(let query): "Nothing matched \(query)"
    case .error(let error): "Error: \(error)"
    }
}
```

`UsersState`, `GroupsState` and the others follow the same shape.

> [tab] SwiftUI
>
> The store is `@Observable`, so a view that reads `store.state` updates with it:
>
> ```swift theme={null}
> struct ChatsWithCount: View {
>     @State private var store = ConversationsStore()
>
>     var body: some View {
>         VStack {
>             Text("\(store.state.entries.count) conversations")
>             CometChatConversations(store: store)
>         }
>     }
> }
> ```

> [tab] UIKit
>
> Use the lifecycle callbacks — or Swift's `withObservationTracking` on the store:
>
> ```swift theme={null}
> conversations
>     .set(onLoad: { list in print("loaded", list.count) })
>     .set(onEmpty: { print("no conversations") })
>     .set(onError: { error in print(error.errorDescription) })
> ```

***

## Configuring Data Fetching

Pass a request builder to control what the component fetches.

> [tab] SwiftUI
>
> A closure that returns a **fresh** builder — it is called for every fetch:
>
> ```swift theme={null}
> CometChatConversations(
>     conversationsRequestBuilder: {
>         ConversationRequest.ConversationRequestBuilder(limit: 20)
>             .setConversationType(conversationType: .group)
>     }
> )
> ```

> [tab] UIKit
>
> A builder:
>
> ```swift theme={null}
> conversations.set(conversationsRequestBuilder:
>     ConversationRequest.ConversationRequestBuilder(limit: 20)
>         .setConversationType(conversationType: .group))
> ```

> [!WARNING]
>
> Pass the builder, not the result of `.build()`. The store builds the request itself, so it
> can rebuild it for a refresh.

***

## List Operations

The Users and Groups stores offer these list operations:

| Method | Description |
| - | - |
| `addItem(_:)`, `addItems(_:)` | append |
| `removeItem(_:)`, `removeItem(at:)` | remove |
| `updateItem(_:matching:)` (Users), `updateItem(_:)` (Groups) | replace a row |
| `moveItemToTop(_:)` | move a row to the top, adding it when missing |
| `items`, `item(at:)`, `itemCount` | read |
| `clearItems()` | empty the list |
| `batch(_:)` | several changes, published once |

```swift theme={null}
let store = UsersStore()
store.batch { users in
    users.removeAll { $0.blockedByMe }
}
```

The UIKit Conversations screen has mutators, which forward to its store:

> [tab] UIKit
>
> ```swift theme={null}
> if let pinned = conversations.getConversationList().first {
>     conversations.remove(conversation: pinned)
>     conversations.insert(conversation: pinned)   // back at the top
> }
> print(conversations.getConversationList().count)
> ```

`ConversationsStore` itself offers `refresh()`, `loadMore()`, `delete(_:)`, `scrollToTop()` and
`sdkConversation(for:)`. The list never filters itself: search opens
[`CometChatSearch`](https://www.cometchat.com/docs/ui-kit/ios/v6/search).

***

## Store Methods

> [tab] SwiftUI
>
> A SwiftUI view is a value, so call methods on the store you passed in:
>
> ```swift theme={null}
> struct ChatsWithRefresh: View {
>     @State private var store = ConversationsStore()
>
>     var body: some View {
>         CometChatConversations(store: store)
>             .toolbar {
>                 Button("Refresh") { Task { await store.refresh() } }
>                 Button("Top") { store.scrollToTop() }
>             }
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> conversations.refreshConversations()
> Task { await conversations.conversationsStore.refresh() }
> ```

| `ConversationsStore` | Description |
| - | - |
| `refresh()` | re-fetch, keeping the rows on screen |
| `loadMore()` | the next page |
| `delete(_:)` | delete a conversation on the server |
| `scrollToTop()` | ask the list to return to the top |
| `policy` | the [behaviour hooks](https://www.cometchat.com/docs/ui-kit/ios/v6/customization-overview.md#policies-instead-of-overrides) |
| `disableSoundForMessages`, `customSoundForMessages` | the incoming sound |
| `textFormatters` | the last-message line's formatters |

***

## Changing a Store's Behaviour

The list stores are `public`, not `open`: from your app you change their behaviour through
their **policy** and properties rather than by subclassing — see
[Policies instead of overrides](https://www.cometchat.com/docs/ui-kit/ios/v6/customization-overview.md#policies-instead-of-overrides).
The satellite stores `CometChatThreadHeaderStore`, `CometChatFlagMessageStore` and
`CometChatMessageInformationStore` are `open`.

***

## Seeded Stores for Previews and Tests

Every store has `init(seeded:)`: it renders the state you give it and performs no fetching,
so a preview or test needs no session.

> [tab] SwiftUI
>
> ```swift theme={null}
> #Preview {
>     CometChatConversations(store: .init(seeded: .populated(entries, hasMore: false)))
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let preview = CometChatConversations(conversationsStore: ConversationsStore(seeded: .empty))
> ```

***

## Lifecycle Callbacks

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatConversations(
>     onLoad: { list in print("loaded", list.count) },
>     onEmpty: { print("no conversations") },
>     onError: { error in print(error) }
> )
> ```

> [tab] UIKit
>
> ```swift theme={null}
> conversations
>     .set(onLoad: { list in print("loaded", list.count) })
>     .set(onEmpty: { print("no conversations") })
> ```

***

## Related

* [Customization Overview](https://www.cometchat.com/docs/ui-kit/ios/v6/customization-overview.md) — the layers and the policies.
* [Events & Callbacks](https://www.cometchat.com/docs/ui-kit/ios/v6/customization-events.md) — taps and the Kit's events.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.