优化 AppDelegate+ThirdConfig 中 NIMSDK 配置,确保生产环境启用 HTTPS,新增 CDN 统计回调和最小时间间隔设置,提升代码可维护性和功能完整性。同时,重构 GiftComboManager,新增后台处理队列和网络处理队列,优化定时器和请求处理逻辑,确保 UI 回调在主线程执行,增强用户体验和代码可读性。

This commit is contained in:
edwinQQQ
2025-08-19 16:33:22 +08:00
parent 83e26bdbae
commit 6d4061bea5
2 changed files with 144 additions and 71 deletions

View File

@@ -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
}

View File

@@ -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.250.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]]) {
// infometadata
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