我最近添加線程到應用程序,以便網絡請求不會阻止用戶界面。在這樣做的時候,我發現我不能再像實施線程之前那樣設置實例變量了。我的實例變量是一個屬性聲明如下:實例變量和線程與GCD
@property (nonatomic, strong) NSMutableArray *currentTopPlaces;
這裏是我如何正確設置我的實例變量self.currentTopPlaces:
dispatch_queue_t downloadQueue = dispatch_queue_create("Flickr Top Places Downloader", NULL);
dispatch_async(downloadQueue, ^{
__block NSArray *topPlaces = [FlickrFetcher topPlaces];
dispatch_async(dispatch_get_main_queue(), ^{
self.tableRowCount = [topPlaces count];
[[self currentTopPlaces] setArray:topPlaces];
});
使用[自currentTopPlace] setArray:topPlaces]在運行良好阻止版本,在我開始使用GCD之前。
現在,我必須將它像這樣的事情才能正常工作:
dispatch_queue_t downloadQueue = dispatch_queue_create("Flickr Top Places Downloader", NULL);
dispatch_async(downloadQueue, ^{
__block NSArray *topPlaces = [FlickrFetcher topPlaces];
dispatch_async(dispatch_get_main_queue(), ^{
self.tableRowCount = [topPlaces count];
self.currentTopPlaces = topPlaces;
});
有人能向我解釋兩者的區別:
[[self currentTopPlaces] setArray:topPlaces];
和:
self.currentTopPlaces = topPlaces;
具體而言,爲什麼「setArray」調用在螺紋塊中不起作用?
我認爲Objective-C中的點符號是語法糖而不是強制性的。我想知道實現相同行爲的「不含糖」的方式。
你是什麼意思的「沒有工作」?請具體說明。 – 2012-08-07 21:23:10