IOS后台线程更新UI的一个陷阱

先看代码

这是一段异步下载图片并更新UI的代码

  1. @interface GXAlertView : UIView {  
  2. @private  
  3.     UIImageView *_imageView;  
  4.     UIActivityIndicatorView *_indicatorView;  
  5. }  


  1. - (void)asyncLoadUrl:(NSString *)aUrl  
  2. {  
  3.     NSURL *imageURL = [NSURL URLWithString:aUrl];  
  4.     [imageURL retain];  
  5.       
  6.     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);  
  7.     dispatch_async(queue, ^{  
  8.         NSData *imageData = [NSData dataWithContentsOfURL:imageURL];  
  9.         [imageURL release];  
  10.         [imageData retain];  
  11.           
  12.         dispatch_sync(dispatch_get_main_queue(), ^{  
  13.             if (imageData) {  
  14.                 _imageView.image = [UIImage imageWithData:imageData];  
  15.                 [imageData release];  
  16.             }  
  17.               
  18.             [_indicatorView stopAnimating];  
  19.             [_indicatorView removeFromSuperview];  
  20.             [_indicatorView release];  
  21.             _indicatorView = nil;  
  22.         });  
  23.     });  
  24. }  

跑一下看看也正常,不过,你要是就以为ok了,那就麻烦了。



崩溃了!!!!

XCode永远停在了XXXView的dealloc里,这是为什么呢?看下调用栈就明白了。

没有想到啊,果然是在后台线程的block成了压垮了GXAlertView某个对象的最后一根稻草(retainCount == 0)。至于为什么crash, log说得很清楚



怎么办

解决办法很直接,别访问self就行了(访问实例成员变量和函数会隐含访问self)

  1. - (void)asyncLoadUrl:(NSString *)aUrl  
  2. {  
  3.     NSURL *imageURL = [NSURL URLWithString:aUrl];  
  4.     [imageURL retain];  
  5.       
  6.     // changes. here  
  7.     UIImageView *imageView = [_imageView retain];  
  8.     UIActivityIndicatorView *indicatorView = [_indicatorView retain];  
  9.       
  10.     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);  
  11.     dispatch_async(queue, ^{  
  12.         NSData *imageData = [NSData dataWithContentsOfURL:imageURL];  
  13.         [imageURL release];  
  14.         [imageData retain];  
  15.           
  16.         dispatch_sync(dispatch_get_main_queue(), ^{  
  17.             if (imageData) {  
  18.                 imageView.image = [UIImage imageWithData:imageData];  
  19.                 [imageData release];  
  20.             }  
  21.               
  22.             [indicatorView stopAnimating];  
  23.             [indicatorView removeFromSuperview];  
  24.             [indicatorView release];  
  25.             [imageView release];  
  26.         });  
  27.     });  
  28. }  


小结

block访问self会增加self的引用计数。

所以的UIKit操作都最好都放到主线程去。

release view也算访问UIKit。

在后台线程直接访问UIKit太危险。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值