嗨,我正在尝试将队列和块用于多线程,但我无法从这些块中访问属性。当我使用 NSLog 打印 self.photoArray 的值时,我得到空值。有人可以解释我做错了什么吗?我试过这个
dispatch_queue_t downloadQueue = dispatch_queue_create("flickrDownload", NULL);
dispatch_async(downloadQueue, ^{
NSArray* photos = [FlickrPhotoSort sortTopPlaces];
dispatch_async(dispatch_get_main_queue(), ^{
self.photoArray = photos; });
});
dispatch_release(downloadQueue);
而这不使用主队列。
dispatch_queue_t downloadQueue = dispatch_queue_create("flickrDownload", NULL);
dispatch_async(downloadQueue, ^{
NSArray* photos = [FlickrPhotoSort sortTopPlaces];
self.photoArray = photos;
});
dispatch_release(downloadQueue);
这是 photoArray 的设置器
-(void) setPhotoArray:(NSArray *)photoArray{
if(!_photoArray) _photoArray = [[NSArray alloc] initWithArray:photoArray];
else
_photoArray = photoArray;
}
我正在关注斯坦福的在线课程,老师只是这样称呼该物业,它似乎有效
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader", NULL);
dispatch_async(downloadQueue, ^{
NSArray *photos = [FlickrFetcher recentGeoreferencedPhotos];
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationItem.rightBarButtonItem = sender;
self.photos = photos;
});
});
dispatch_release(downloadQueue);