【发布时间】:2012-03-06 13:34:06
【问题描述】:
应用程序由导航视图组成。 我在 viewDidAppear 中调用一个函数,它是
[self extractInfoWithWebsite:[NSString stringWithFormat:@"http://www.cineklik.com/%@-%@.aspx", self.cinemaType, self.location]];
此函数从 Internet 上的当前网页进行 HTML 解析。 共享代码有点长,但其想法是从网站中提取数据。
我遇到的问题是,如果用户决定在加载信息之前单击导航的后退按钮,则页面保持不变并且应用程序停止运行(不是崩溃而是变成静态的,不会'不要再做任何事情了)。
有解决办法吗? 提前谢谢你
编辑:
我将代码更改为以下内容: NSOperationQueue *queue = [NSOperationQueue 新];
/* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(extractInfoWithWebsite:)
object:[NSString stringWithFormat:@"http://www.cineklik.com/%@-%@.aspx", self.cinemaType, self.location]];
/* Add the operation to the queue */
[queue addOperation:operation];
[operation release];
现在我可以按下返回按钮而不会冻结,但现在发生了崩溃并出现以下错误: 试图从除主线程或 web 线程之外的线程获取 web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃... 我认为这是因为我的功能仍在后台运行。 如何解决这个问题?
【问题讨论】:
-
您真的要“取消”请求吗? // 另一种解决方案(过程)可能是您使用 bgthread 之类的东西来执行这些“繁重”操作 - (当使用单独的线程时,您的主线程不会锁定)
-
如果正在加载信息,您应该禁用用户交互并在完成时启用用户交互。
-
另一种解决方案是在导航返回时取消加载数据
标签: iphone ios xcode html-parsing