修复聊天最后一条跑到最上面的问题
This commit is contained in:
@@ -177,19 +177,17 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.currentPage = 1;
|
||||
self.hasMoreHistory = YES;
|
||||
|
||||
// ✅ 尝试从缓存加载 messages
|
||||
NSArray *cachedMessages = [[KBAIChatMessageCacheManager shared] messagesForCompanionId:persona.personaId];
|
||||
if (cachedMessages.count > 0) {
|
||||
// 缓存命中,直接使用
|
||||
self.messages = [cachedMessages mutableCopy];
|
||||
self.hasLoadedData = YES;
|
||||
NSLog(@"[Cell] ✅ 从缓存加载:personaId=%ld, 消息数=%ld", (long)persona.personaId, (long)cachedMessages.count);
|
||||
} else {
|
||||
// 缓存未命中,需要请求
|
||||
// ⚠️ 临时禁用缓存,排查问题
|
||||
// NSArray *cachedMessages = [[KBAIChatMessageCacheManager shared] messagesForCompanionId:persona.personaId];
|
||||
// if (cachedMessages.count > 0) {
|
||||
// self.messages = [cachedMessages mutableCopy];
|
||||
// self.hasLoadedData = YES;
|
||||
// NSLog(@"[Cell] ✅ 从缓存加载:personaId=%ld, 消息数=%ld", (long)persona.personaId, (long)cachedMessages.count);
|
||||
// } else {
|
||||
self.messages = [NSMutableArray array];
|
||||
self.hasLoadedData = NO;
|
||||
NSLog(@"[Cell] ⚠️ 缓存未命中:personaId=%ld, 需要请求数据", (long)persona.personaId);
|
||||
}
|
||||
NSLog(@"[Cell] ⚠️ 缓存已禁用:personaId=%ld, 需要请求数据", (long)persona.personaId);
|
||||
// }
|
||||
|
||||
// 设置 UI
|
||||
[self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:persona.coverImageUrl]
|
||||
@@ -205,6 +203,12 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
// 确保开场白在第一条
|
||||
[self ensureOpeningMessageAtTop];
|
||||
|
||||
NSLog(@"[KBPersonaChatCell] ========== setPersona 调试 ==========");
|
||||
NSLog(@"[KBPersonaChatCell] personaId: %ld", (long)persona.personaId);
|
||||
NSLog(@"[KBPersonaChatCell] messages.count: %ld", (long)self.messages.count);
|
||||
NSLog(@"[KBPersonaChatCell] chatView.frame: %@", NSStringFromCGRect(self.chatView.frame));
|
||||
NSLog(@"[KBPersonaChatCell] contentView.frame: %@", NSStringFromCGRect(self.contentView.frame));
|
||||
|
||||
// 如果有消息,直接显示(包含开场白)
|
||||
if (self.messages.count > 0) {
|
||||
// 同步缓存,避免下次从缓存缺少开场白
|
||||
@@ -217,6 +221,8 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
[self.chatView clearMessages];
|
||||
}
|
||||
|
||||
NSLog(@"[KBPersonaChatCell] ========== setPersona 结束 ==========");
|
||||
|
||||
[self.commentButton setTitle:persona.commentCount forState:UIControlStateNormal];
|
||||
[self.likeButton setTitle:persona.likeCount forState:UIControlStateNormal];
|
||||
self.likeButton.selected = persona.liked;
|
||||
@@ -294,7 +300,10 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
}
|
||||
|
||||
// 插入历史消息(确保开场白始终是第一条)
|
||||
if (weakSelf.currentPage == 1) {
|
||||
// 关键修复:在 dispatch_async 之前保存当前页码,避免异步执行时 currentPage 已经被递增
|
||||
NSInteger loadedPage = weakSelf.currentPage;
|
||||
|
||||
if (loadedPage == 1) {
|
||||
// 第一页,直接赋值
|
||||
weakSelf.messages = newMessages;
|
||||
[weakSelf ensureOpeningMessageAtTop];
|
||||
@@ -310,15 +319,26 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
|
||||
// 刷新 UI
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
BOOL keepOffset = (weakSelf.currentPage != 1);
|
||||
BOOL scrollToBottom = (weakSelf.currentPage == 1);
|
||||
[weakSelf.chatView reloadWithMessages:weakSelf.messages
|
||||
keepOffset:keepOffset
|
||||
scrollToBottom:scrollToBottom];
|
||||
[weakSelf.chatView endLoadMoreWithHasMoreData:weakSelf.hasMoreHistory];
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
if (!strongSelf) {
|
||||
NSLog(@"[KBPersonaChatCell] ⚠️ strongSelf 为空,跳过刷新");
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用保存的页码判断,而不是 currentPage(可能已被递增)
|
||||
BOOL keepOffset = (loadedPage != 1);
|
||||
BOOL scrollToBottom = (loadedPage == 1);
|
||||
|
||||
NSLog(@"[KBPersonaChatCell] 刷新 UI - loadedPage: %ld, keepOffset: %d, scrollToBottom: %d",
|
||||
(long)loadedPage, keepOffset, scrollToBottom);
|
||||
|
||||
[strongSelf.chatView reloadWithMessages:strongSelf.messages
|
||||
keepOffset:keepOffset
|
||||
scrollToBottom:scrollToBottom];
|
||||
[strongSelf.chatView endLoadMoreWithHasMoreData:strongSelf.hasMoreHistory];
|
||||
|
||||
// ✅ 保存到缓存(包含开场白)
|
||||
[[KBAIChatMessageCacheManager shared] saveMessages:weakSelf.messages
|
||||
[[KBAIChatMessageCacheManager shared] saveMessages:strongSelf.messages
|
||||
forCompanionId:companionId];
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user