优化 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] setShouldSyncStickTopSessionInfos:YES];
[NIMSDKConfig sharedConfig].shouldConsiderRevokedMessageUnreadCount = YES;
///
[[NIMSDKConfig sharedConfig] setShouldSyncStickTopSessionInfos: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;
@@ -114,8 +123,8 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
//
- (void)reset {
NSLog(@"[Combo effect] 🔄 开始连击重置 - combo: %ld -> 1, enableCombo: %@, actionCallback: %@",
(long)self.combo,
NSLog(@"[Combo effect] 🔄 开始连击重置 - combo: %ld -> 1, enableCombo: %@, actionCallback: %@",
(long)self.combo,
self.enableCombo ? @"YES" : @"NO",
self.actionCallback ? @"可用" : @"为空");
@@ -129,10 +138,14 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
// GiftComboView
[[NSNotificationCenter defaultCenter] postNotificationName:@"ComboCountReset" object:nil];
//
// - 线UI
if (self.actionCallback && self.enableCombo) {
NSLog(@"[Combo effect] 📱 触发连击面板显示回调");
self.actionCallback(ComboAction_ShowPanel);
@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元素");
self.handleRoomUIChanged(YES);
@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] 📱 触发连击面板移除回调");
self.actionCallback(ComboAction_RemovePanel);
@kWeakify(self);
[self safeExecuteUIBlock:^{
@kStrongify(self);
self.actionCallback(ComboAction_RemovePanel);
}];
}
}
@@ -237,7 +258,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
[self clearAllQueues];
// 3.
NSLog(@"[Combo effect] 🔄 重置状态标志 - isCombing: %@ -> NO",
NSLog(@"[Combo effect] 🔄 重置状态标志 - isCombing: %@ -> NO",
self.isCombing ? @"YES" : @"NO");
self.isCombing = NO;
@@ -254,7 +275,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
// 5.
NSLog(@"[Combo effect] 📢 发送强制重置通知");
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:kBoomStateForceResetNotification
[[NSNotificationCenter defaultCenter] postNotificationName:kBoomStateForceResetNotification
object:nil];
});
@@ -265,8 +286,8 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
self.handleRoomUIChanged(NO);
});
}
NSLog(@"[Combo effect] ✅ 强制重置完成 - enableCombo保持: %@, actionCallback保持: %@",
self.enableCombo ? @"YES" : @"NO",
NSLog(@"[Combo effect] ✅ 强制重置完成 - enableCombo保持: %@, actionCallback保持: %@",
self.enableCombo ? @"YES" : @"NO",
self.actionCallback ? @"可用" : @"为空");
}
@@ -299,11 +320,11 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
//
- (BOOL)isComboStateValid {
@synchronized (self) {
return self.isCombing &&
self.enableCombo &&
self.combo > 0 &&
self.giftInfo != nil &&
self.sendGiftToUIDs.count > 0;
return self.isCombing &&
self.enableCombo &&
self.combo > 0 &&
self.giftInfo != nil &&
self.sendGiftToUIDs.count > 0;
}
}
@@ -373,12 +394,12 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
}
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
dispatch_source_set_timer(timer,
DISPATCH_TIME_NOW,
0.2 * NSEC_PER_SEC,
0.01 * NSEC_PER_SEC);
DISPATCH_TIME_NOW,
0.2 * NSEC_PER_SEC,
0.01 * NSEC_PER_SEC);
@kWeakify(self);
dispatch_source_set_event_handler(timer, ^{
@@ -407,7 +428,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
if (self.uiQueue.count == 0) {
return;
}
GiftReceiveInfoModel *receiveInfo = [self.uiQueue firstObject];
NSLog(@"[Combo effect] 🎪 处理连击飘屏 - combo: %ld, giftId: %ld", (long)receiveInfo.comboCount, (long)receiveInfo.gift.giftId);
[self.uiQueue xpSafeRemoveObjectAtIndex:0];
@@ -427,12 +448,12 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
//
return;
}
if (self.activeViews.count >= 2) {
GiftComboFlagView *oldestView = [self.activeViews firstObject];
[self animateRemoveView:oldestView];
}
CGFloat positionY = kGetScaleWidth(380);
CGFloat positionX = isMSRTL() ? -self.containerView.bounds.size.width : self.containerView.bounds.size.width;
GiftComboFlagView *flagView = [[GiftComboFlagView alloc] initWithFrame:CGRectMake(positionX,
@@ -447,12 +468,12 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
[self animateRemoveView:flagView];
}];
[flagView updateReceiveInfoModel:receiveInfo animationType:0];
[self allCurrentFlagMoveDown];
[self.containerView addSubview:flagView];
[self.activeViews insertObject:flagView atIndex:0];
[self animateView:flagView positionY:positionY];
}
@@ -473,10 +494,10 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
if (isMSRTL()) {
//
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
// x - -
CGFloat newX = screenWidth - 20 - kGetScaleWidth(300);
// flagView frame使 20
flagView.frame = CGRectMake(newX, positionY, kGetScaleWidth(300), 50);
} else {
@@ -493,7 +514,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
[self.activeViews removeObject:flagView];
flagView = nil;
}
[self allCurrentFlagMoveDown];
}
@@ -510,7 +531,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
}
index++;
}
return NO;
}
@@ -527,25 +548,25 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
if (self.timer) {
return; //
}
// GCD
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
//#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
//
// 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.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];
});
// ****
// ****
[self processRequestQueue];
[self processGiftComboQueue];
@@ -559,7 +580,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
if (self.requestQueue.count == 0 && self.networkQueue.count == 0) {
//
dispatch_source_cancel(self.timer);
// timer nil
@kWeakify(self);
dispatch_source_set_cancel_handler(self.timer, ^{
@@ -572,15 +593,17 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
//
- (void)processGiftComboQueue {
@synchronized (self) {
@synchronized (self) {
if (self.networkQueue.count > 0) {
//
id networkData = [self.networkQueue firstObject];
[self.networkQueue xpSafeRemoveObjectAtIndex:0];
//
if ([networkData isKindOfClass:[AttachmentModel class]]) {
[self processGiftComboWith:(AttachmentModel *)networkData];
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) {
[self handleSendGift:dic];
// API线
dispatch_async(self.networkProcessingQueue, ^{
[self handleSendGift:dic];
});
[self.requestQueue removeObject:dic];
}
} else {
@@ -623,14 +649,14 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
// save
- (void)configureWithGiftInfo:(GiftInfoModel *)giftInfo
targetUIDs:(NSArray *)UIDs
roomUID:(NSString *)roomUID
sessionID:(NSString *)sessionID
userInfo:(UserInfoModel *)userInfo
targetUIDs:(NSArray *)UIDs
roomUID:(NSString *)roomUID
sessionID:(NSString *)sessionID
userInfo:(UserInfoModel *)userInfo
countModel:(XPGiftCountModel *)countModel
sourceType:(GiftSourceType)sourceType
sendType:(RoomSendGiftType)sendType
giftNum:(NSString *)giftNum {
sendType:(RoomSendGiftType)sendType
giftNum:(NSString *)giftNum {
NSLog(@"[Combo effect] 🔧 统一配置连击参数");
@@ -644,7 +670,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
self.roomSendGiftType = sendType;
self.giftNumPerTimes = giftNum;
NSLog(@"[Combo effect] ✅ 连击参数配置完成 - giftId: %ld, targetCount: %ld",
NSLog(@"[Combo effect] ✅ 连击参数配置完成 - giftId: %ld, targetCount: %ld",
(long)giftInfo.giftId, (long)UIDs.count);
}
@@ -664,7 +690,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
}
allUIDs = [allUIDs stringByAppendingString:item];
}
NSDictionary *dic = @{
@"targetUids":allUIDs,
@"giftNum":self.giftNumPerTimes,
@@ -674,7 +700,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
@"giftType":[NSString stringWithFormat:@"%ld", self.roomSendGiftType],
@"roomUid":self.roomUID
};
NSLog(@"[Combo effect] 📦 添加礼物请求到队列 - giftId: %ld, targetUids: %@", (long)self.giftInfo.giftId, allUIDs);
[self.requestQueue addObject:dic];
[self startProcessingQueue];
@@ -753,8 +779,13 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
}
}
// 线UI
if (self.actionCallback) {
self.actionCallback(ComboAction_Error);
@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) {
self.actionCallback(ComboAction_Combo_Count_Update);
@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) {
self.actionCallback(ComboAction_Update_After_Send_Success);
@kWeakify(self);
[self safeExecuteUIBlock:^{
@kStrongify(self);
self.actionCallback(ComboAction_Update_After_Send_Success);
}];
}
NSLog(@"[Combo effect] ✅ 连击礼物处理完成");
@@ -820,10 +860,12 @@ 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;
UserInfoModel *userInfo = self.sendGiftUserInfo;
XPMessageRemoteExtModel *extModel = [[XPMessageRemoteExtModel alloc] init];
extModel.androidBubbleUrl = userInfo.androidBubbleUrl;
@@ -832,18 +874,39 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
extModel.platformRole = userInfo.platformRole;
NSMutableDictionary *remoteExt = [NSMutableDictionary dictionaryWithObject:extModel.model2dictionary forKey:[NSString stringWithFormat:@"%ld", userInfo.uid]];
message.remoteExt = remoteExt;
//
NIMSession *session = [NIMSession session:self.sessionID type:NIMSessionTypeChatroom];
NSError *error = nil;
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:&error];
if (error) {
NSLog(@"[Combo effect] ❌ 云信消息发送失败 - error: %@", error.localizedDescription);
// 线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 {
NSLog(@"[Combo effect] ✅ 云信消息发送成功");
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