删除测试数据
This commit is contained in:
@@ -85,7 +85,6 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
|
||||
- (void)dealloc {
|
||||
[self stopPasteboardMonitor];
|
||||
[self kb_stopNetworkStreaming];
|
||||
[self kb_stopMockStreaming];
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
}
|
||||
|
||||
@@ -241,13 +240,12 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
|
||||
self.streamDeleteButton = del;
|
||||
|
||||
// 优先拉取后端测试数据(GET);失败则回落到本地演示
|
||||
[self kb_startNetworkStreamingWithSeed:title fallbackToMock:YES];
|
||||
[self kb_startNetworkStreamingWithSeed:title];
|
||||
}
|
||||
|
||||
- (void)kb_onTapStreamDelete {
|
||||
// 关闭并销毁流式视图,恢复标签列表
|
||||
[self kb_stopNetworkStreaming];
|
||||
[self kb_stopMockStreaming];
|
||||
[self.streamDeleteButton removeFromSuperview];
|
||||
self.streamDeleteButton = nil;
|
||||
[self.streamTextView removeFromSuperview];
|
||||
@@ -255,67 +253,18 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
|
||||
self.collectionViewInternal.hidden = NO;
|
||||
}
|
||||
|
||||
#pragma mark - Mock Streaming
|
||||
|
||||
// 生成并按时间片推送模拟流数据到 KBStreamTextView
|
||||
- (void)kb_startMockStreamingWithSeed:(NSString *)seedTitle {
|
||||
[self kb_stopMockStreaming];
|
||||
|
||||
// 连续文本源(包含 \t 作为“分段”分隔符;\n 为换行),按 1~3 字随机切片推送
|
||||
NSString *t = seedTitle ?: @"高情商";
|
||||
self.streamMockSource = [NSString stringWithFormat:
|
||||
@"选择了标签:%@\n"
|
||||
"下面演示流式输出,每隔一小段时间来一截。第一段结束。\t"
|
||||
"第二段开始:这是一个较长的句子,会被拆成多个小块推送,直到遇到分隔符就换段。\n第二段结束。\t"
|
||||
"最后一段:右上角可随时关闭展示。\n演示完成。\t",
|
||||
t];
|
||||
self.streamMockCursor = 0;
|
||||
self.streamMockChunks = nil; // 废弃数组方式
|
||||
self.streamMockIndex = 0;
|
||||
|
||||
// 定时喂数据(主线程):每次随机 1~3 个字
|
||||
__weak typeof(self) weakSelf = self;
|
||||
self.streamMockTimer = [NSTimer scheduledTimerWithTimeInterval:0.20 repeats:YES block:^(NSTimer * _Nonnull timer) {
|
||||
__strong typeof(weakSelf) self = weakSelf; if (!self) { [timer invalidate]; return; }
|
||||
if (!self.streamTextView) { [timer invalidate]; return; }
|
||||
NSInteger total = (NSInteger)self.streamMockSource.length;
|
||||
if (self.streamMockCursor >= total) {
|
||||
// 结束:收尾裁剪当前段
|
||||
[self.streamTextView finishStreaming];
|
||||
[timer invalidate];
|
||||
self.streamMockTimer = nil;
|
||||
return;
|
||||
}
|
||||
// 随机 1~3 个字(不跨越结尾),逐段追加
|
||||
uint32_t step = arc4random_uniform(3) + 1; // 1..3
|
||||
NSInteger remain = total - self.streamMockCursor;
|
||||
NSInteger take = MIN((NSInteger)step, remain);
|
||||
NSString *piece = [self.streamMockSource substringWithRange:NSMakeRange(self.streamMockCursor, take)];
|
||||
self.streamMockCursor += take;
|
||||
[self.streamTextView appendStreamText:piece ?: @""];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)kb_stopMockStreaming {
|
||||
[self.streamMockTimer invalidate];
|
||||
self.streamMockTimer = nil;
|
||||
self.streamMockChunks = nil;
|
||||
self.streamMockIndex = 0;
|
||||
self.streamMockSource = nil;
|
||||
self.streamMockCursor = 0;
|
||||
}
|
||||
|
||||
#pragma mark - Network Streaming (GET)
|
||||
|
||||
// 后端测试地址(内网)。若被 ATS 拦截,请在扩展 target 的 Info.plist 中允许 HTTP 或添加例外域。
|
||||
static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/talk";
|
||||
|
||||
- (void)kb_startNetworkStreamingWithSeed:(NSString *)seedTitle fallbackToMock:(BOOL)fallback {
|
||||
- (void)kb_startNetworkStreamingWithSeed:(NSString *)seedTitle {
|
||||
[self kb_stopNetworkStreaming];
|
||||
if (![[KBFullAccessManager shared] hasFullAccess]) { if (fallback) /*[self kb_startMockStreamingWithSeed:seedTitle]*/; return; }
|
||||
if (![[KBFullAccessManager shared] hasFullAccess]) { return; }
|
||||
|
||||
NSURL *url = [NSURL URLWithString:kKBStreamDemoURL];
|
||||
if (!url) { if (fallback) [self kb_startMockStreamingWithSeed:seedTitle]; return; }
|
||||
if (!url) { return; }
|
||||
|
||||
self.streamHasOutput = NO; // 重置首段处理标记
|
||||
__weak typeof(self) weakSelf = self;
|
||||
@@ -333,9 +282,9 @@ static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/
|
||||
};
|
||||
fetcher.onFinish = ^(NSError * _Nullable error) {
|
||||
__strong typeof(weakSelf) self = weakSelf; if (!self) return;
|
||||
if (error && fallback && !self.streamHasOutput) {
|
||||
[KBHUD showInfo:@"拉取失败,使用本地演示"]; // 降级
|
||||
[self kb_startMockStreamingWithSeed:nil];
|
||||
if (error) {
|
||||
[KBHUD showInfo:@"拉取失败"];
|
||||
[self.streamTextView finishStreaming];
|
||||
} else {
|
||||
[self.streamTextView finishStreaming];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user