iPhone app crashes with no justifiable reason?
I am developing an app in which I have a table. In the table cell I have an imageview ( the images are displayed via url ) and a textview/webview. I start threads for each row to get the images in the
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method ( if image is not already got ) and set the text of textview/webview from an array.
The problem arises when the images are being received and I pop the view, the application crashes giving the following message:
bool _WebTryThreadLock(bool), 0x1a0670: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
Now 开发者_Python百科the situation becomes more weird if I don't release the textview/webview that I added to the cell, then every thing works fine.
Edit: the crash does not happens when I replace textview/webview with a label
Hoping I am clear in my question. If any thing is confusing please comment. I need to resolve this.
Thanks,
Nikhil
That's a huge mistake to use threads. Try to avoid using threads if you have other solutions !
In your case, just use an asynchronous NSURLConnection which will take care of downloading your image while not slowing your app ;)
Here is part of the code :
- (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];
}
精彩评论