Custom Data
Confused about "Custom Data"?
Let us know how we can improve our documentation:
Changing the default extra data types with your custom types is easy and takes a couple of easy steps.
For example, let's say you want to replace the default NoExtraData
of ChatChannel
with your custom NameAndColorExtraData
:
1. Define your custom Channel extra data type
Copied!Confused about "[object Object]"?
Let us know how we can improve our documentation:
Your custom data type must conform to the ChannelExtraData
protocol. The protocol has two requirements: the type must be codable, and it must expose a static defaultValue
variable.
1
2
3
4
5
6
7
8
9
/// Your custom ChatChannel extra data type
struct NameAndColorExtraData: ChannelExtraData {
/// The value used when decoding the custom data type fails.
static var defaultValue = NameAndColorExtraData(name: "Unknown", colorName: nil)
let name: String
let colorName: String?
}
2. Use the type in your custom implementation of ExtraDataTypes
Copied!Confused about "[object Object]"?
Let us know how we can improve our documentation:
1
2
3
4
5
6
7
8
import StreamChat
/// Custom implementation of `ExtraDataTypes` with `NameAndColorExtraData`
enum MyCustomExtraData: ExtraDataTypes {
typealias Channel = NameAndColorExtraData
// Note: Unless you specify other custom data types, the default data types are used.
}
3. Define the following typealiases in your module
Copied!Confused about "[object Object]"?
Let us know how we can improve our documentation:
You should define the convenience typealiases in the module where you use StreamChat
. You can copy&paste the snippet below, and replace MyCustomExtraData
with the type defined in step 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import StreamChat
// Change this typealias to your custom data types
typealias CustomExtraDataTypes = MyCustomExtraData // 👈 Your_Custom_Data_Type_Here 👈
typealias ChatClient = _ChatClient<CustomExtraDataTypes>
typealias ChatUser = _ChatUser<CustomExtraDataTypes.User>
typealias CurrentChatUser = _CurrentChatUser<CustomExtraDataTypes.User>
typealias ChatChannel = _ChatChannel<CustomExtraDataTypes>
typealias ChatChannelRead = _ChatChannelRead<CustomExtraDataTypes>
typealias ChatChannelMember = _ChatChannelMember<CustomExtraDataTypes.User>
typealias ChatMessage = _ChatMessage<CustomExtraDataTypes>
typealias CurrentChatUserController = _CurrentChatUserController<CustomExtraDataTypes>
typealias ChatChannelListController = _ChatChannelListController<CustomExtraDataTypes>
typealias ChatChannelController = _ChatChannelController<CustomExtraDataTypes>
typealias ChatMessageController = _ChatMessageController<CustomExtraDataTypes>
Important
Copied!Confused about "Important"?
Let us know how we can improve our documentation:
Extra data is embedded directly to the root object, not nested under any extraData
object.
So if you have such a Channel object:
1
2
3
4
5
{
'id': ....,
// all other default fields
'color': 'red'
}