请先尝试self.topPlaces像这样存根:
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr topPlace", NULL);
dispatch_async(downloadQueue, ^{
NSArray *topPlaces = [FlickrFetcher topPlaces];
dispatch_async(dispatch_get_main_queue(), ^{
self.topPlaces = @[@"test", @"test2", @"test3"];
});
});
然后检查 的值self.topPlaces。如果仍然NULL如此,那么我需要询问您的财产self.topPlaces有什么终身限定符(例如强、弱、分配)?如果是,weak那么在分配它之后,它的值当然topPlaces会是NULL,因为不会有任何强指针指向它。如果是,则执行到达时strong的值NSArray *topPlaces = [FlickrFetcher topPlaces];是。NULLself.topPlaces = topPlaces;
要考虑的另一件事是,当您执行异步操作时,主线程上的执行将继续执行。因此,如果您正在执行以下操作...
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr topPlace", NULL);
dispatch_async(downloadQueue, ^{
NSArray *topPlaces = [FlickrFetcher topPlaces];
dispatch_async(dispatch_get_main_queue(), ^{
self.topPlaces = topPlaces;
});
});
NSLog(@"topPlaces = %@", self.topPlaces);
然后我希望它self.topPlaces总是NULL在它到达时NSLog因为它不会被设置,直到 after[FlickrFetcher topPlaces]完成并返回并且执行继续到dispatch_async(dispatch_get_main_queue().... 此时应设置该值。您可能需要执行以下操作,以确保您不仅设置属性,而且在异步操作完成后执行某种更新操作以更新 UI...
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr topPlace", NULL);
dispatch_async(downloadQueue, ^{
NSArray *topPlaces = [FlickrFetcher topPlaces];
dispatch_async(dispatch_get_main_queue(), ^{
[self updateUIWithTopPlaces:topPlaces];
});
});
- (void)updateUIWithTopPlaces:(NSArray*)topPlaces {
self.topPlaces = topPlaces;
// Perform your UI updates here
}