Storage
RTM 的 Storage 功能提供了一套动态数据库机制,可以让开发者动态设置、存储、更新、删除 Channel Metadata 和 User Metadata 等数据。
setChannelMetadata
接口描述
setChannelMetadata
方法可以为频道(Message Channel 或 Stream Channel)设置 Channel Metadata。一个频道只能有一组 Metadata,但每组 Metadata 可以有一个或多个 Metadata Item。如果多次调用该方法,SDK 会依次检索 Metadata Item 的 key
值,并按如下规则处理:
- 如果设置的 Metadata Item
key
值不同,则会根据调用顺序依次增加。 - 如果设置的 Metadata Item
key
值相同,则最后一次设置的 Metadata 的value
会覆盖之前的值。
成功设置 Channel Metadata 后,订阅该频道且开启事件监听的用户会收到 AgoraRtmStorageTypeChannel
类型的 didReceiveStorageEvent
事件通知。详见事件监听。
Channel Metadata 同时也引进了版本控制逻辑 CAS(Compare And Set),该方法提供两种独立的版本控制字段,你可以根据实际业务场景设置任意一种或多种:
- 通过
AgoraRtmMetadata
中的setMajorRevision
方法设置majorRevision
属性开启整组 Channel Metadata 的版本号校验。 - 通过
AgoraRtmMetadataItem
中的revision
属性开启某个 Metadata Item 的版本号校验。
设置 Channel Metadata 或 Metadata Item 时,配合版本属性可以控制本次调用是否开启版本号校验,逻辑如下:
- 版本属性为
-1
时,本次调用不开启 CAS 验证。如果 Channel Metadata 或 Metadata Item 已存在,则该值会被最新值覆盖;如果 Channel Metadata 或 Metadata Item 不存在,则 SDK 会创建该值。 - 版本属性为正整数时,本次调用开启 CAS 验证。如果 Channel Metadata 或 Metadata Item 已存在,则 SDK 会在版本号验证成功后更新对应的值;如果 Channel Metadata 或 Metadata Item 不存在,则 SDK 会返回错误码。
接口方法
你可以通过以下方式调用 setChannelMetadata
方法:
- (void) setChannelMetadata: (NSString * _Nonnull)channelName
channelType: (AgoraRtmChannelType)channelType
data: (AgoraRtmMetadata* _Nonnull)data
options: (AgoraRtmMetadataOptions* _Nullable)options
lock: (NSString * _Nullable)lock
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
channelName | NSString | 必填 | - | 频道名称。 |
channelType | AgoraRtmChannelType | 必填 | - | 频道类型。详见 AgoraRtmChannelType 。 |
data | AgoraRtmMetadata | 必填 | - | Metadata Item。详见 AgoraRtmMetadata 。 |
options | AgoraRtmMetadataOptions | 选填 | - | Channel Metadata 配置选项。 |
lock | NSString | 选填 | 空字符串 '' | Lock 名称。设置后,只有调用 acquireLock 方法获取该锁的用户才能执行操作。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmMetadataOptions
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
recordTs | BOOL | 选填 | false | 是否记录编辑的时间戳。 |
recordUserId | BOOL | 选填 | false | 是否记录编辑者的用户 ID。 |
基本用法
//get metadata
AgoraRtmMetadata* metadata = [[rtm getStorage] createMetadata];
//set metadata items
AgoraRtmMetadataItem* properties = [[AgoraRtmMetadataItem alloc] init];
properties.key = @"Quantity";
properties.value = @"20";
AgoraRtmMetadataItem* announcement = [[AgoraRtmMetadataItem alloc] init];
announcement.key = @"Announcement";
announcement.value = @"Welcome to our shop!";
AgoraRtmMetadataItem* price = [[AgoraRtmMetadataItem alloc] init];
price.key = @"T-shirt";
price.value = @"100";
[metadata setMetadataItem:properties];
[metadata setMetadataItem:announcement];
[metadata setMetadataItem:price];
AgoraRtmMetadataOptions* metadata_opt = [[AgoraRtmMetadataOptions alloc] init];
metadata_opt.recordUserId = true;
metadata_opt.recordTs = true;
[[rtm getStorage] setChannelMetadata:@"your_channel" channelType:AgoraRtmChannelTypeMessage data:metadata options:metadata_opt lock:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"setChannelMetadata success!!");
} else {
NSLog(@"setChannelMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
getChannelMetadata
接口描述
getChannelMetadata
方法可以获取指定频道的 Metadata。
接口方法
你可以通过以下方式调用 getChannelMetadata
方法:
- (void) getChannelMetadata: (NSString * _Nonnull)channelName
channelType: (AgoraRtmChannelType)channelType
completion: (AgoraRtmGetMetadataBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
channelName | NSString | 必填 | - | 频道名称。 |
channelType | AgoraRtmChannelType | 必填 | - | 频道类型。详见 AgoraRtmChannelType 。 |
completionBlock | AgoraRtmGetMetadataBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmGetMetadataResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmGetMetadataResponse
数据类型包含以下属性:
属性 | 类型 | 描述 |
---|---|---|
data | AgoraRtmMetadata | Metadata Item。详见 AgoraRtmMetadata 。 |
基本用法
[[rtm getStorage] getChannelMetadata:@"your_channel" channelType:AgoraRtmChannelTypeMessage completion:^(AgoraRtmGetMetadataResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"getChannelMetadata success!!");
AgoraRtmMetadata* data = response.data; //get storage data;
NSArray<AgoraRtmMetadataItem *> * items = [data getMetadataItems];
for (int i = 0; i < items.count; i++) {
NSLog(@"key: %@ value: %@ revison: %lld", items[i].key, items[i].value, items[i].revision);
}
} else {
NSLog(@"getChannelMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
removeChannelMetadata
接口描述
removeChannelMetadata
方法可以删除 Channel Metadata 或 Channel Metadata Item 数组。
删除时,配合版本属性可以控制本次调用是否开启版本号校验,逻辑如下:
- 版本属性为
-1
时,本次调用不开启 CAS 验证。如果 Channel Metadata 或 Metadata Item 已存在,则 SDK 会删除该值;如果 Channel Metadata 或 Metadata Item 不存在,则 SDK 会返回错误码。 - 版本属性为正整数时,本次调用开启 CAS 验证。如果 Channel Metadata 或 Metadata Item 已存在,则 SDK 会在版本号验证成功后删除对应值;如果 Channel Metadata 或 Metadata Item 不存在,则 SDK 会返回错误码。
成功删除 Channel Metadata 或 Metadata Item 后,订阅该频道且开启事件监听的用户会收到 AgoraRtmStorageTypeChannel
类型的 didReceiveStorageEvent
事件通知。详见事件监听。
接口方法
你可以通过以下方式调用 removeChannelMetadata
方法:
- (void) removeChannelMetadata: (NSString * _Nonnull)channelName
channelType: (AgoraRtmChannelType)channelType
data: (AgoraRtmMetadata* _Nonnull)data
options: (AgoraRtmMetadataOptions* _Nullable)options
lock: (NSString * _Nullable)lock
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
channelName | NSString | 必填 | - | 频道名称。 |
channelType | AgoraRtmChannelType | 必填 | - | 频道类型。详见 AgoraRtmChannelType 。 |
data | AgoraRtmMetadata | 必填 | - | Metadata Item。详见 AgoraRtmMetadata 。 |
options | AgoraRtmMetadataOptions | 选填 | - | Channel Metadata 配置选项。 |
lock | NSString | 选填 | 空字符串 '' | Lock 名称。设置后,只有调用 acquireLock 方法获取该锁的用户才能执行操作。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmMetadataOptions
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
recordTs | BOOL | 选填 | false | 是否记录编辑的时间戳。 |
recordUserId | BOOL | 选填 | false | 是否记录编辑者的用户 ID。 |
基本用法
AgoraRtmMetadata* metadata = [[rtm getStorage] createMetadata];
AgoraRtmMetadataItem* properties = [[AgoraRtmMetadataItem alloc] init];
properties.key = @"Quantity";
[metadata setMetadataItem:properties];
[[rtm getStorage] removeChannelMetadata:@"your_channel" channelType:AgoraRtmChannelTypeMessage data:metadata options:metadata_opt lock:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"removeChannelMetadata success!!");
} else {
NSLog(@"removeChannelMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
updateChannelMetadata
接口描述
updateChannelMetadata
方法可以更新已有的 Channel Metadata。每次调用该方法,你可以更新一个 Channel Metadata,也可以更新一个或多个 Channel Metadata Item 数组。
成功更新后,订阅该频道且开启事件监听的用户会收到 AgoraRtmStorageTypeChannel
类型的 didReceiveStorageEvent
事件通知。详见事件监听。
该方法不能对不存在的 Metadata Item 进行操作。
接口方法
你可以通过以下方式调用 updateChannelMetadata
方法:
- (void) updateChannelMetadata: (NSString * _Nonnull)channelName
channelType: (AgoraRtmChannelType)channelType
data: (AgoraRtmMetadata* _Nonnull)data
options: (AgoraRtmMetadataOptions* _Nullable)options
lock: (NSString * _Nullable)lock
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
channelName | NSString | 必填 | - | 频道名称。 |
channelType | AgoraRtmChannelType | 必填 | - | 频道类型。详见 AgoraRtmChannelType 。 |
data | AgoraRtmMetadata | 必填 | - | Metadata Item。详见 AgoraRtmMetadata 。 |
options | AgoraRtmMetadataOptions | 选填 | - | Channel Metadata 配置选项。 |
lock | NSString | 选填 | 空字符串 '' | Lock 名称。设置后,只有调用 acquireLock 方法获取该锁的用户才能执行操作。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmMetadataOptions
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
recordTs | BOOL | 选填 | false | 是否记录编辑的时间戳。 |
recordUserId | BOOL | 选填 | false | 是否记录编辑者的用户 ID。 |
基本用法
//get metadata
AgoraRtmMetadata* metadata = [[rtm getStorage] createMetadata];
//set metadata items
AgoraRtmMetadataItem* price = [[AgoraRtmMetadataItem alloc] init];
price.key = @"T-shirt";
price.value = @"299";
[metadata setMetadataItem:price];
AgoraRtmMetadataOptions* metadata_opt = [[AgoraRtmMetadataOptions alloc] init];
metadata_opt.recordUserId = true;
metadata_opt.recordTs = true;
[[rtm getStorage] updateChannelMetadata:@"your_channel" channelType:AgoraRtmChannelTypeMessage data:metadata options:metadata_opt lock:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"updateChannelMetadata success!!");
} else {
NSLog(@"updateChannelMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
setUserMetadata
接口描述
setUserMetadata
方法可以设置 User Metadata。如果多次调用该方法时,SDK 会依次检索 User Metadata Item 的 key
值,并按如下规则处理:
- 如果设置的 Metadata Item key 值不同,则会根据调用顺序依次增加。
- 如果设置的 Metadata Item key 值相同,则最后一次设置的 Metadata 的 value 会覆盖之前的值。
成功设置 User Metadata 后,订阅该用户 Metadata 且开启事件监听的用户会收到 AgoraRtmStorageTypeUser
类型的 didReceiveStorageEvent
事件通知。详见事件监听。
User Metadata 同时也引进了版本控制逻辑 CAS,该方法提供两种独立的版本控制字段,你可以根据实际业务场景设置任意一种或多种:
- 通过
AgoraRtmMetadata
中的setMajorRevision
方法设置majorRevision
属性开启整组 Channel Metadata 的版本号校验。 - 通过
AgoraRtmMetadataItem
中的revision
属性开启某个 Metadata Item 的版本号校验。
设置 User Metadata 时,配合版本属性可以控制本次调用是否开启版本号校验,逻辑如下:
- 版本属性为
-1
时,本次调用不开启 CAS 验证。如果 User Metadata 或 Metadata Item 已存在,则该值会被最新值覆盖;如果 User Metadata 或 Metadata Item 不存在,则会创建该值。 - 版本属性为正整数时,本次调用开启 CAS 验证。如果 User Metadata 或 Metadata Item 已存在,则 SDK 会在版本号验证成功后更新对应的值;如果 User Metadata 或 Metadata Item 不存在,则 SDK 会返回错误码。
接口方法
你可以通过以下方式调用 setUserMetadata
方法:
- (void) setUserMetadata: (NSString * _Nonnull)userId
data: (AgoraRtmMetadata* _Nonnull)data
options: (AgoraRtmMetadataOptions* _Nullable)options
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
userId | NSString | 必填 | 当前用户的 userId | 用户 ID。 |
data | AgoraRtmMetadata | 必填 | - | Metadata Item。详见 AgoraRtmMetadata 。 |
options | AgoraRtmMetadataOptions | 选填 | - | Channel Metadata 配置选项。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmMetadataOptions
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
recordTs | BOOL | 选填 | false | 是否记录编辑的时间戳。 |
recordUserId | BOOL | 选填 | false | 是否记录编辑者的用户 ID。 |
基本用法
AgoraRtmMetadata* metadata = [[rtm getStorage] createMetadata];
AgoraRtmMetadataItem* Name = [[AgoraRtmMetadataItem alloc] init];
properties.key = @"Name";
properties.value = @"Tony";
AgoraRtmMetadataItem* Age = [[AgoraRtmMetadataItem alloc] init];
announcement.key = @"Age";
announcement.value = @"40";
AgoraRtmMetadataItem* Avatar = [[AgoraRtmMetadataItem alloc] init];
price.key = @"Avatar";
price.value = @"https://your-domain/avatar/tong.png";
[metadata setMetadataItem:Name];
[metadata setMetadataItem:Age];
[metadata setMetadataItem:Avatar];
AgoraRtmMetadataOptions* metadata_opt = [[AgoraRtmMetadataOptions alloc] init];
metadata_opt.recordUserId = true;
metadata_opt.recordTs = true;
[[rtm getStorage] setUserMetadata:@"userId" data:metadata options:metadata_opt completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"setUserMetadata success!!");
} else {
NSLog(@"setUserMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
getUserMetadata
接口描述
getUserMetadata
方法可以获取指定用户的 Metadata 和 User Metadata Item。
接口方法
你可以通过以下方式调用 getUserMetadata
方法:
- (void) getUserMetadata: (NSString * _Nonnull)userId
completion: (AgoraRtmGetMetadataBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
userId | NSString | 必填 | 当前用户的 userId | 用户 ID。 |
completionBlock | AgoraRtmGetMetadataBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmGetMetadataResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmGetMetadataResponse
数据类型包含以下属性:
属性 | 类型 | 描述 |
---|---|---|
data | AgoraRtmMetadata | Metadata Item。详见 AgoraRtmMetadata 。 |
基本用法
[[rtm getStorage] getUserMetadata:@"userId" completion:^(AgoraRtmGetMetadataResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"getUserMetadata success!!");
AgoraRtmMetadata* data = response.data; //get storage data;
NSArray<AgoraRtmMetadataItem *> * items = [data getMetadataItems];
for (int i = 0; i < items.count; i++) {
NSLog(@"key: %@ value: %@ revison: %lld", items[i].key, items[i].value, items[i].revision);
}
} else {
NSLog(@"getUserMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
removeUserMetadata
接口描述
removeUserMetadata
方法可以删除 User Metadata 或某几个 User Metadata Item。
成功删除后,订阅该用户 Metadata 且开启事件监听的用户会收到 AgoraRtmStorageTypeUser
类型的 didReceiveStorageEvent
事件通知。详见事件监听。
接口方法
你可以通过以下方式调用 removeUserMetadata
方法:
- (void) removeUserMetadata: (NSString * _Nonnull)userId
data: (AgoraRtmMetadata* _Nonnull)data
options: (AgoraRtmMetadataOptions* _Nullable)options
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
userId | NSString | 必填 | 当前用户的 userId | 用户 ID。 |
data | AgoraRtmMetadata | 必填 | - | Metadata Item。详见 AgoraRtmMetadata 。 |
options | AgoraRtmMetadataOptions | 选填 | - | Channel Metadata 配置选项。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmMetadataOptions
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
recordTs | BOOL | 选填 | false | 是否记录编辑的时间戳。 |
recordUserId | BOOL | 选填 | false | 是否记录编辑者的用户 ID。 |
基本用法
AgoraRtmMetadata* metadata = [[rtm getStorage] createMetadata];
AgoraRtmMetadataItem* Age = [[AgoraRtmMetadataItem alloc] init];
announcement.key = @"Age";
[metadata setMetadataItem:Age];
[[rtm getStorage] removeUserMetadata:@"userId" data:metadata options:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"removeUserMetadata success!!");
} else {
NSLog(@"removeUserMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
updateUserMetadata
接口描述
updateUserMetadata
方法可以更新已有的 User Metadata 或 Metadata Item。成功更新后,订阅该 User Metadata 且开启事件监听的用户会收到 AgoraRtmStorageTypeUser
类型的 didReceiveStorageEvent
事件通知。详见事件监听。
该方法不能对不存在的 Metadata Item 进行操作。
接口方法
你可以通过以下方式调用 updateUserMetadata
方法:
- (void) updateUserMetadata: (NSString * _Nonnull)userId
data: (AgoraRtmMetadata* _Nonnull)data
options: (AgoraRtmMetadataOptions* _Nullable)options
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
userId | NSString | 必填 | 当前用户的 userId | 用户 ID。 |
data | AgoraRtmMetadata | 必填 | - | Metadata Item。详见 AgoraRtmMetadata 。 |
options | AgoraRtmMetadataOptions | 选填 | - | Channel Metadata 配置选项。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
AgoraRtmMetadataOptions
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
recordTs | BOOL | 选填 | false | 是否记录编辑的时间戳。 |
recordUserId | BOOL | 选填 | false | 是否记录编辑者的用户 ID。 |
基本用法
AgoraRtmMetadata* metadata = [[rtm getStorage] createMetadata];
AgoraRtmMetadataItem* Age = [[AgoraRtmMetadataItem alloc] init];
announcement.key = @"Age";
announcement.value = @"45";
[metadata setMetadataItem:Age];
AgoraRtmMetadataOptions* metadata_opt = [[AgoraRtmMetadataOptions alloc] init];
metadata_opt.recordUserId = true;
metadata_opt.recordTs = true;
[[rtm getStorage] updateUserMetadata:@"userId" data:metadata options:metadata_opt completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"updateUserMetadata success!!");
} else {
NSLog(@"updateUserMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
subscribeUserMetadata
接口描述
subscribeUserMetadata
方法可以订阅指定用户的 Metadata。成功订阅且开启事件监听后,当该用户的 Metadata 发生变更时会收到 AgoraRtmStorageTypeUser
类型的 didReceiveStorageEvent
事件通知。详见事件监听。
接口方法
你可以通过以下方式调用 subscribeUserMetadata
方法:
- (void) subscribeUserMetadata: (NSString * _Nonnull)userId
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
userId | NSString | 必填 | 当前用户的 userId | 用户 ID。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
基本用法
[[rtm getStorage] subscribeUserMetadata:@"userId" completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"subscribeUserMetadata success!!");
} else {
NSLog(@"subscribeUserMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
unsubscribeUserMetadata
接口描述
如果你不需要接收某个用户 User Metadata 的变更通知,调用unsubscribeUserMetadata
方法取消订阅。
接口方法
你可以通过以下方式调用 unsubscribeUserMetadata
方法:
- (void) unsubscribeUserMetadata: (NSString * _Nonnull)userId
completion: (AgoraRtmOperationBlock _Nullable)completionBlock;
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
userId | NSString | 必填 | 当前用户的 userId | 用户 ID。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:response 参数中返回 AgoraRtmCommonResponse 数据,在 errorInfo 参数中返回 nil 。response 参数中返回 nil ,在 errorInfo 参数中返回错误信息。 |
基本用法
[[rtm getStorage] unsubscribeUserMetadata:@"userId" completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"unsubscribeUserMetadata success!!");
} else {
NSLog(@"unsubscribeUserMetadata failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
AgoraRtmMetadata
接口描述
接口方法
你可以通过以下方式调用相关方法:
__attribute__((visibility("default"))) @interface AgoraRtmMetadata: NSObject
- (instancetype _Nullable)init NS_UNAVAILABLE;
- (void) setMajorRevision: (long long)revision;
- (long long) getMajorRevision;
- (void) setMetadataItem: (AgoraRtmMetadataItem* _Nonnull)item;
- (NSArray<AgoraRtmMetadataItem *> * _Nonnull) getMetadataItems;
- (void) clearMetadata;
- (int) destroy;
@end
方法 | 描述 |
---|---|
init | |
createMetadata | |
setMajorRevision | 设置版本控制开关: -1 : 关闭版本校验。0 : 开启版本校验,只有目标版本号与此值相符才能执行操作。 |
getMajorRevision | 获取 Major Revision。 |
setMetadataItem | |
getMetadataItems | |
clearMetadata | |
destroy | 销毁 AgoraRtmMetadata 实例。 |
AgoraRtmMetadataItem
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
key | NSString | 必填 | - | 键。 |
value | NSString | 必填 | - | 值。 |
authorUserId | NSString | 必填 | - | 编辑者的用户 ID。该值为只读,不支持写入。 |
revision | long long | 选填 | -1 |
|
updateTs | long long | 选填 | 0 | 更新时间戳。该值为只读,不支持写入。 |
基本用法
// set MetadataItem and set MajorRevision
AgoraRtmMetadataItem* properties = [[AgoraRtmMetadataItem alloc] init];
properties.key = @"Quantity";
properties.value = @"20";
properties.revision = 1000;
[metadata setMetadataItem:properties];
[metadata setMajorRevision:100];
// get MetadataItems;
NSArray<AgoraRtmMetadataItem *> * items = [metadata getMetadataItems];
for (int i = 0; i < items.count; i++) {
NSLog(@"key: %@ value: %@ revison: %lld", items[i].key, items[i].value, items[i].revision);
}
long long major_revison = [metadata getMajorRevision];
// clear all set in AgoraRtmMetadata
[metadata clearMetadata];