gpt4 book ai didi

ios - 旧 View 的线程仍在运行

转载 作者:可可西里 更新时间:2023-11-01 05:54:53 29 4
gpt4 key购买 nike

我有一个有五个屏幕的应用。

在每个屏幕上,我在 viewDidLoad 中从服务器访问数据。

在每个屏幕上我都有下一个按钮。

当我从屏幕一转到屏幕五(通过单击下一步 4 次)时,在 NSLog 中,我仍然看到所有前四个 View Controller 完成的过程。

有什么办法,我怎样才能杀死那些线程?

简而言之,当我离开该 View 时,我不想执行任何过程,即如果我从 View 3 转到 4,我想停止我为 View 3 执行的任务.

获取早期 View 的数据并等待该数据(这是不需要的)对应用程序不利,因此我想要像我上面解释的那样。


编辑1

下面是我用来读取数据的代码。

.h

@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableData *data;

使用下面我请求数据

.m

NSString *myTMainURL = [NSString stringWithFormat:@"http://www.google.com"];
NSURL *url = [NSURL URLWithString:myTMainURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

关于阅读,下面是我的阅读方式。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
data = [[NSMutableData alloc] init ];
[webData setLength: 0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
NSLog(@"didReceiveData");
[data appendData:theData];
[webData appendData:data];
NSLog(@"didreceveidata leng===%d===%d", [webData length], [data length]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");

NSString *myDataFromLink = [[NSString alloc] initWithBytes: [data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding];

NSLog(@"myDataFromLink====%@--", myDataFromLink);

}

最佳答案

viewWillDisappear: 中,将 cancel 发送到任何正在运行的操作。

当然,这假设您一个可取消任务/方法/操作。

例如,对于网络请求,如果您使用 NSURLConnection,这就是您采用委托(delegate)方法时的情况。使用 NSURLConnection 方便的类方法 sendAsynchronousRequest:queue:completionHandler: 这是不可能的。因此,任何严肃的应用程序都会使用委托(delegate)方法,因为长时间运行的异步操作必须是可取消的。

关于ios - 旧 View 的线程仍在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369009/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com