ios block一定會犯的幾個錯誤


貼幾段斯坦福大學關於gcd的代碼,這段代碼逐步演示了如何修正錯誤,其中用到的既是串行隊列
 
1。這個是原始代碼
  1. - (void)viewWillAppear:(BOOL)animated  
  2. {  
  3.     NSData *imageData = [FlickrFetcher imageDataForPhotoWithURLString:photo.URL];  
  4.     UIImage *image = [UIImage imageWithData:imageData];  
  5.     self.imageView.image = image;  
  6.     self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);  
  7.     self.scrollView.contentSize = image.size;  
  8. }  

2。這個是采用gcdd的代碼,里面有錯誤3處
  1. - (void)viewWillAppear:(BOOL)animated  
  2. {  
  3.     dispatch_queue_t downloadQueue = dispatch_queue_create(“Flickr downloader”, NULL);  
  4.     dispatch_async(downloadQueue, ^{  
  5.          NSData *imageData = [FlickrFetcher imageDataForPhotoWithURLString:photo.URL];  
  6.          UIImage *image = [UIImage imageWithData:imageData];  
  7.          self.imageView.image = image;  
  8.          self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);  
  9.          self.scrollView.contentSize = image.size;  
  10.     });  
  11. }  

3。第一個錯誤, UI更新只能在主線程中 Problem! UIKit calls can only happen in the main thread!
改正后如下:
  1. - (void)viewWillAppear:(BOOL)animated  
  2. {  
  3.     dispatch_queue_t downloadQueue = dispatch_queue_create(“Flickr downloader”, NULL);  
  4.     dispatch_async(downloadQueue, ^{  
  5.          NSData *imageData = [FlickrFetcher imageDataForPhotoWithURLString:photo.URL];  
  6.          <span style="color:#ff0000;">dispatch_async(dispatch_get_main_queue(), ^{</span>  
  7.              UIImage *image = [UIImage imageWithData:imageData];  
  8.              self.imageView.image = image;  
  9.              self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);  
  10.              self.scrollView.contentSize = image.size;  
  11.          });  
  12. }); }  

4。第二個錯誤,NSManagedObjectContext並不是線程安全的,gcd中訪問成員變量有危險
Problem! NSManagedObjectContext is not thread safe,
so we can’t call photo.URL in downloadQueue’s t

改正后如下:
  1. - (void)viewWillAppear:(BOOL)animated  
  2. {  
  3.   <span style="color:#ff0000;">  NSString *url = photo.URL;</span>  
  4.     dispatch_queue_t downloadQueue = dispatch_queue_create(“Flickr downloader”, NULL);  
  5.     dispatch_async(downloadQueue, ^{  
  6.         NSData *imageData = [FlickrFetcher <span style="color:#ff0000;">imageDataForPhotoWithURLString:url];</span>  
  7.         dispatch_async(dispatch_get_main_queue(), ^{  
  8.             UIImage *image = [UIImage imageWithData:imageData];  
  9.             self.imageView.image = image;  
  10.             self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);  
  11.             self.scrollView.contentSize = image.size;  
  12. }); });  
  13. }  
5。第三個錯誤,隊列創建后沒有釋放,內存泄露
  1. - (void)viewWillAppear:(BOOL)animated  
  2. {  
  3.     NSString *url = photo.URL;  
  4.     dispatch_queue_t downloadQueue = dispatch_queue_create(“Flickr downloader”, NULL);  
  5.     dispatch_async(downloadQueue, ^{  
  6.         NSData *imageData = [FlickrFetcher imageDataForPhotoWithURLString:url];  
  7.         dispatch_async(dispatch_get_main_queue(), ^{  
  8.             UIImage *image = [UIImage imageWithData:imageData];  
  9.             self.imageView.image = image;  
  10.             self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);  
  11.             self.scrollView.contentSize = image.size;  
  12. }); });  
  13. <span style="color:#ff0000;">dispatch_release(downloadQueue); //won’tactuallygoawayuntilqueueisemp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM