Files
peko-ios/current_user_mic_switch_flow.md

99 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 当前用户切换 mic 时云信消息发送流程
## 修复后的完整流程
```
1. 用户切换 mic (位置A → 位置B)
2. 接收云信通知 NIMChatroomEventTypeQueueChange
- 下麦通知 (changeType == 2)
- 上麦通知 (changeType == 1)
3. 调用 handleMicChangeForCP:queue
4. 在 handleMicChangeForCP 中执行:
a) updateCurrentUserMicStatus:queue
- 检测到麦位变化
- 设置 currentUserMicStatusChanged = YES
b) updateMicMidpointRectManagerSnapshot
c) handleDownMicEventIfNeeded:queue
d) handleMicSwitchScenarioIfNeeded:queue
- 检测到 currentUserMicStatusChanged = YES
- 清除所有CP关系
- 重新构建CP关系
- 🔧 不重置状态标志(关键修复)
e) callMicCpListByUidListOnMicChangeWithQueue:queue
- 调用 micCpListByUidList API
5. API 成功回调 getMicCpListByUidListSuccess:cpList
6. 在 getMicCpListByUidListSuccess 中:
a) 更新 CP 缓存
b) 刷新绘制
c) 检查 currentUserMicStatusChanged = YES
d) 🔧 发送云信消息 sendMicRelationshipNIMessage:cpList
e) 重置状态标志 currentUserMicStatusChanged = NO
7. 其他用户收到云信消息更新CP关系
```
## 关键修复点
### 修复前的问题
- `handleMicSwitchScenarioIfNeeded` 中提前重置 `currentUserMicStatusChanged = NO`
- API 成功回调时状态标志已经是 `NO`
- 无法发送云信消息
### 修复后的逻辑
- `handleMicSwitchScenarioIfNeeded` 中不重置状态标志
- 状态标志保持 `YES` 直到 API 成功回调
- API 成功回调时正确发送云信消息
- 发送消息后重置状态标志
## 代码修改
### 修改前
```objc
// 在 handleMicSwitchScenarioIfNeeded 中
if (hasMicPositionChanged) {
// 处理麦位变化场景
[midpointRectManager removeAllMidpointRects];
[midpointRectManager rebuildMicSnapshotWithStageView:self.stageView micCount:micCount];
[self drawSocialStageMidpointRects];
// ❌ 提前重置状态标志
self.currentUserMicStatusChanged = NO;
}
```
### 修改后
```objc
// 在 handleMicSwitchScenarioIfNeeded 中
if (hasMicPositionChanged) {
// 处理麦位变化场景
[midpointRectManager removeAllMidpointRects];
[midpointRectManager rebuildMicSnapshotWithStageView:self.stageView micCount:micCount];
[self drawSocialStageMidpointRects];
// ✅ 不在这里重置状态标志等待API成功回调后再重置
// 状态标志将在 getMicCpListByUidListSuccess 中重置,确保能发送云信消息
}
```
## 验证要点
1. **当前用户切换mic时**
- ✅ 正确检测到麦位状态变化
- ✅ 重新构建所有CP关系
- ✅ 调用 micCpListByUidList API
- ✅ API成功回调时发送云信消息
- ✅ 状态标志在发送消息后正确重置
2. **其他用户切换mic时**
- ✅ 通过 handleOtherUserMicChange 处理
- ✅ 不发送云信消息(由切换用户发送)
3. **边界情况**
- ✅ 当前用户不在麦上时清理CP数据
- ✅ 只有当前用户一个人时清理CP数据