优化 AppDelegate+ThirdConfig 中 NIMSDK 配置,确保生产环境启用 HTTPS,新增 CDN 统计回调和最小时间间隔设置,提升代码可维护性和功能完整性。同时,重构 GiftComboManager,新增后台处理队列和网络处理队列,优化定时器和请求处理逻辑,确保 UI 回调在主线程执行,增强用户体验和代码可读性。
This commit is contained in:
@@ -115,12 +115,22 @@ UIKIT_EXTERN NSString * adImageName;
|
||||
|
||||
// NIM SDK初始化
|
||||
[NIMCustomObject registerCustomDecoder:[[CustomAttachmentDecoder alloc] init]];
|
||||
[NIMSDKConfig sharedConfig].shouldConsiderRevokedMessageUnreadCount = YES;
|
||||
///置顶会话同步
|
||||
[[NIMSDKConfig sharedConfig] setShouldSyncStickTopSessionInfos:YES];
|
||||
[NIMSDKConfig sharedConfig].shouldConsiderRevokedMessageUnreadCount = YES;
|
||||
|
||||
// cdn统计回调不触发
|
||||
[NIMSDKConfig sharedConfig].cdnTrackInterval = 0;
|
||||
|
||||
// 最小时间间隔设置为最小边界值
|
||||
[NIMSDKConfig sharedConfig].chatroomMessageReceiveMinInterval = 50;
|
||||
|
||||
#ifdef DEBUG
|
||||
[NIMSDKConfig sharedConfig].enabledHttpsForInfo = NO;
|
||||
[NIMSDKConfig sharedConfig].enabledHttpsForMessage = NO;
|
||||
#else
|
||||
// 生产环境启用HTTPS
|
||||
[NIMSDKConfig sharedConfig].enabledHttpsForInfo = YES;
|
||||
[NIMSDKConfig sharedConfig].enabledHttpsForMessage = YES;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@@ -41,6 +41,10 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
// 定时器,处理请求的调度器
|
||||
@property (nonatomic, strong) dispatch_source_t timer;
|
||||
|
||||
// 新增:后台处理队列
|
||||
@property (nonatomic, strong) dispatch_queue_t backgroundQueue;
|
||||
@property (nonatomic, strong) dispatch_queue_t networkProcessingQueue;
|
||||
|
||||
@property (nonatomic, copy) NSArray *sendGiftToUIDs;
|
||||
@property (nonatomic, assign) GiftSourceType giftSourceType;
|
||||
@property (nonatomic, strong) GiftInfoModel *giftInfo;
|
||||
@@ -83,6 +87,11 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
sharedInstance.uiQueue = [NSMutableArray array];
|
||||
sharedInstance.activeViews = [NSMutableArray array];
|
||||
sharedInstance.requestQueue = [NSMutableArray array];
|
||||
|
||||
// 初始化后台处理队列
|
||||
sharedInstance.backgroundQueue = dispatch_queue_create("com.yumi.giftcombo.background", DISPATCH_QUEUE_CONCURRENT);
|
||||
sharedInstance.networkProcessingQueue = dispatch_queue_create("com.yumi.giftcombo.network", DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
[sharedInstance startProcessingUIQueue];
|
||||
});
|
||||
return sharedInstance;
|
||||
@@ -129,10 +138,14 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
// 发送通知,让 GiftComboView 重置显示
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:@"ComboCountReset" object:nil];
|
||||
|
||||
// 检查是否应该显示连击面板
|
||||
// 检查是否应该显示连击面板 - 确保在主线程执行UI回调
|
||||
if (self.actionCallback && self.enableCombo) {
|
||||
NSLog(@"[Combo effect] 📱 触发连击面板显示回调");
|
||||
@kWeakify(self);
|
||||
[self safeExecuteUIBlock:^{
|
||||
@kStrongify(self);
|
||||
self.actionCallback(ComboAction_ShowPanel);
|
||||
}];
|
||||
self.isCombing = YES;
|
||||
} else if (self.actionCallback && !self.enableCombo) {
|
||||
NSLog(@"[Combo effect] ⚠️ enableCombo为NO,不显示连击面板");
|
||||
@@ -142,7 +155,11 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
|
||||
if (self.handleRoomUIChanged) {
|
||||
NSLog(@"[Combo effect] 🎮 隐藏房间UI元素");
|
||||
@kWeakify(self);
|
||||
[self safeExecuteUIBlock:^{
|
||||
@kStrongify(self);
|
||||
self.handleRoomUIChanged(YES);
|
||||
}];
|
||||
}
|
||||
NSLog(@"[Combo effect] ✅ 连击重置完成 - isCombing: %@", self.isCombing ? @"YES" : @"NO");
|
||||
}
|
||||
@@ -186,10 +203,14 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
NSLog(@"[Combo effect] 🗑️ 清除连击状态");
|
||||
[self forceBoomStateReset];
|
||||
|
||||
// 通知UI移除连击面板
|
||||
// 通知UI移除连击面板 - 确保在主线程执行UI回调
|
||||
if (self.actionCallback) {
|
||||
NSLog(@"[Combo effect] 📱 触发连击面板移除回调");
|
||||
@kWeakify(self);
|
||||
[self safeExecuteUIBlock:^{
|
||||
@kStrongify(self);
|
||||
self.actionCallback(ComboAction_RemovePanel);
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -528,18 +549,18 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
return; // 如果定时器已经在运行,直接返回
|
||||
}
|
||||
|
||||
// 创建 GCD 定时器
|
||||
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
|
||||
// 创建 GCD 定时器 - 使用后台队列避免主线程阻塞
|
||||
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.backgroundQueue);
|
||||
|
||||
//#if DEBUG
|
||||
// dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC, 0.01 * NSEC_PER_SEC);
|
||||
//#else
|
||||
// 设置定时器时间间隔:每 0.25 秒执行一次
|
||||
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC, 0.01 * NSEC_PER_SEC);
|
||||
//#endif
|
||||
//#if DEBUG
|
||||
// dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC, 0.01 * NSEC_PER_SEC);
|
||||
//#else
|
||||
// 优化:减少间隔提高响应速度,从0.25秒改为0.1秒
|
||||
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC, 0.01 * NSEC_PER_SEC);
|
||||
//#endif
|
||||
|
||||
|
||||
// 定时器触发的事件处理
|
||||
// 定时器触发的事件处理 - 在后台队列执行
|
||||
dispatch_source_set_event_handler(self.timer, ^{
|
||||
[self processRequestQueue];
|
||||
[self processGiftComboQueue];
|
||||
@@ -580,7 +601,9 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
|
||||
// 处理逻辑
|
||||
if ([networkData isKindOfClass:[AttachmentModel class]]) {
|
||||
dispatch_async(self.networkProcessingQueue, ^{
|
||||
[self processGiftComboWith:(AttachmentModel *)networkData];
|
||||
});
|
||||
} else if ([networkData isKindOfClass:[NSDictionary class]]) {
|
||||
// 处理包含info和metadata的字典
|
||||
NSDictionary *comboData = (NSDictionary *)networkData;
|
||||
@@ -610,7 +633,10 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
// 获取并移除队列中的第一个元数据
|
||||
NSDictionary *dic = [self.requestQueue xpSafeObjectAtIndex:0];
|
||||
if (dic) {
|
||||
// 优化:在后台队列处理API请求,避免阻塞主线程
|
||||
dispatch_async(self.networkProcessingQueue, ^{
|
||||
[self handleSendGift:dic];
|
||||
});
|
||||
[self.requestQueue removeObject:dic];
|
||||
}
|
||||
} else {
|
||||
@@ -753,8 +779,13 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
}
|
||||
}
|
||||
|
||||
// 确保在主线程执行UI回调
|
||||
if (self.actionCallback) {
|
||||
@kWeakify(self);
|
||||
[self safeExecuteUIBlock:^{
|
||||
@kStrongify(self);
|
||||
self.actionCallback(ComboAction_Error);
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -781,9 +812,13 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
NSLog(@"[Combo effect] 🔢 API成功,递增连击计数 - 当前: %ld -> %ld", (long)self.combo, (long)(self.combo + 1));
|
||||
self.combo += 1;
|
||||
|
||||
// 更新UI显示
|
||||
// 更新UI显示 - 确保在主线程执行UI回调
|
||||
if (self.actionCallback) {
|
||||
@kWeakify(self);
|
||||
[self safeExecuteUIBlock:^{
|
||||
@kStrongify(self);
|
||||
self.actionCallback(ComboAction_Combo_Count_Update);
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -809,8 +844,13 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
self.handleComboSuccess(receive, dic);
|
||||
}
|
||||
|
||||
// 确保在主线程执行UI回调
|
||||
if (self.actionCallback) {
|
||||
@kWeakify(self);
|
||||
[self safeExecuteUIBlock:^{
|
||||
@kStrongify(self);
|
||||
self.actionCallback(ComboAction_Update_After_Send_Success);
|
||||
}];
|
||||
}
|
||||
|
||||
NSLog(@"[Combo effect] ✅ 连击礼物处理完成");
|
||||
@@ -820,6 +860,8 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
NSLog(@"[Combo effect] 📨 发送云信自定义消息 - combo: %ld", (long)self.combo);
|
||||
|
||||
NIMMessage *message = [[NIMMessage alloc]init];
|
||||
message.setting.quickDeliveryEnabled = YES; // 开启快速投递
|
||||
|
||||
NIMCustomObject *object = [[NIMCustomObject alloc] init];
|
||||
object.attachment = attachment;
|
||||
message.messageObject = object;
|
||||
@@ -835,15 +877,36 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
|
||||
//构造会话
|
||||
NIMSession *session = [NIMSession session:self.sessionID type:NIMSessionTypeChatroom];
|
||||
|
||||
// 优化:确保在主线程发送云信消息,因为云信SDK可能期望在主线程调用
|
||||
[self safeExecuteUIBlock:^{
|
||||
NSError *error = nil;
|
||||
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:&error];
|
||||
|
||||
if (error) {
|
||||
NSLog(@"[Combo effect] ❌ 云信消息发送失败 - error: %@", error.localizedDescription);
|
||||
} else {
|
||||
NSLog(@"[Combo effect] ✅ 云信消息发送成功");
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
// 新增:辅助方法,统一处理UI回调的线程安全
|
||||
- (void)safeExecuteUIBlock:(void (^)(void))uiBlock {
|
||||
if (!uiBlock) return;
|
||||
|
||||
if ([NSThread isMainThread]) {
|
||||
uiBlock();
|
||||
} else {
|
||||
dispatch_async(dispatch_get_main_queue(), uiBlock);
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:性能监控方法
|
||||
- (void)logPerformanceMetrics {
|
||||
NSLog(@"[Combo effect] 📊 性能指标 - 网络队列: %lu, 请求队列: %lu, 定时器: %@",
|
||||
(unsigned long)self.networkQueue.count,
|
||||
(unsigned long)self.requestQueue.count,
|
||||
self.timer ? @"运行中" : @"已停止");
|
||||
}
|
||||
|
||||
@end
|
||||
|
Reference in New Issue
Block a user