【问题标题】:iPhone app crashes with no justifiable reason?iPhone 应用程序在没有正当理由的情况下崩溃?
【发布时间】:2010-03-23 23:38:20
【问题描述】:

我正在开发一个应用程序,其中有一张桌子。在表格单元格中,我有一个 imageview(图像通过 url 显示)和一个 textview/webview。我为每一行启动线程以获取

中的图像
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

方法(如果尚未获取图像)并从数组中设置 textview/webview 的文本。

当接收到图像并弹出视图时出现问题,应用程序崩溃并显示以下消息:

bool _WebTryThreadLock(bool), 0x1a0670: 试图从主线程或web线程以外的线程获取web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃了...

如果我不释放我添加到单元格中的 textview/webview,情况会变得更加奇怪,然后一切正常。

编辑:当我用标签替换 textview/webview 时不会发生崩溃

希望我的问题很清楚。如果有任何令人困惑的事情,请发表评论。我需要解决这个问题。

谢谢,

尼基尔

【问题讨论】:

  • 听起来你的一个后台线程正在尝试对用户界面做一些事情,但(通常)只有主线程应该调用用户界面对象上的任何方法。
  • @Isaac :问题本身说明了你所说的,但编辑点非常重要。

标签: iphone iphone-sdk-3.0 crash


【解决方案1】:

使用线程是一个巨大的错误。如果您有其他解决方案,请尽量避免使用线程!

在您的情况下,只需使用异步 NSURLConnection 即可下载您的图像,同时不会减慢您的应用程序;)

这是部分代码:

- (void) startDownload {
    self.activeDownload = [NSMutableData data];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                             [NSURLRequest requestWithURL:
                              [NSURL URLWithString:@"blablabla"]] delegate:self];
    self.imageConnection = conn;
    [conn release];
}

#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.activeDownload appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR DOWNLOADING");
    // Clear the activeDownload property to allow later attempts
    self.activeDownload = nil;

    // Release the connection now that it's finished
    self.imageConnection = nil;
}

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

    UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
    self.activeDownload = nil;
    self.imageConnection = nil;

    //do whatever you want with your image

    [image release];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多