ios - 如果您知道它被调用了 n 次,请等待异步任务完成

标签 ios objective-c iphone objective-c-blocks

在这个项目中,我使用 Foursquare API 获取一些地点和每个地点的一张照片。当我有这些数据时,我将重新加载 TableView 以显示所有信息。

首先,我收集 field ,然后为每个 field ,我从 foursquare API 拍照。

我正在为这个项目使用 RestKit 库,并且我调用了这个方法 n 次(每个地点一次)。完成后,我想将我拍摄的所有照片显示到我的表格 View 中。

- (void)requestVenuePhoto:(Venue *)thisVenue{
  //...
  //...
  [[RKObjectManager sharedManager] getObjectsAtPath:objectPath parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
  {
    [self.photos addObjectsFromArray:mappingResult.array];
    //[self.tableView reloadData];

  } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"What do you mean by 'there is no photos?': %@", error);
    dispatch_group_leave(resolveVenuePhotos);
  }]; 
}

问题是我不能使用 dispatch_group_leave 因为它不被调用一次。 有什么办法可以很好地做到这一点?

更新,现在我用计数器来解决问题:

[[RKObjectManager sharedManager] getObjectsAtPath:objectPath parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
    [self.photos addObjectsFromArray:mappingResult.array];
    venuesPhotoCounter++;
    if (venuesPhotoCounter == _venues.count)
    {
        [self.tableView reloadData];
    }

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"What do you mean by 'there is no photos?': %@", error);
}];

最佳答案

Apple 已上传名为“LazyTableImages”的项目可用 here .因此,我将其总结并制作了一些可能适合您的用例的范例。

 - (void)startDownload
    {
        NSURLRequest *request = [NSURLRequest requestWithURL:self.venue.imageURL];


        // create an session data task to obtain and download the app icon
        _sessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            // in case we want to know the response status code
            NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];


            if (error != nil)
            {
                if ([error code] == NSURLErrorAppTransportSecurityRequiresSecureConnection)
                {
                    // if you get error NSURLErrorAppTransportSecurityRequiresSecureConnection (-1022),
                    // then your Info.plist has not been properly configured to match the target server.
                    //
                    abort();
                }
            }

            [[NSOperationQueue mainQueue] addOperationWithBlock: ^{

                // Set appIcon and clear temporary data/image
                UIImage *image = [[UIImage alloc] initWithData:data];
                if (HTTPStatusCode == 200) {
                    if (image.size.width != kAppIconSize || image.size.height != kAppIconSize)
                    {
                        CGSize itemSize = CGSizeMake(kAppIconSize, kAppIconSize);
                        UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0f);
                        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
                        [image drawInRect:imageRect];
                        self.venue.image = UIGraphicsGetImageFromCurrentImageContext();
                        UIGraphicsEndImageContext();
                    }
                    else
                    {
                        self.venue.image = image;
                    }
                }
                else {// If anything goes wrong we should use a placeholder image
                    self.venue.image = [UIImage imageNamed:@"Placeholder.png"];
                }



                // call our completion handler to tell our client that our icon is ready for display
                if (self.completionHandler != nil)
                {
                    self.completionHandler();
                }
            }];
        }];

        [self.sessionTask resume];
    }

关于ios - 如果您知道它被调用了 n 次,请等待异步任务完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34222860/

相关文章:

iphone - 如何以编程方式判断 IBAction 是否已被代码或用户操作调用

ios - 为什么连接回 iOS 后台 NSURLSession 等待锁定的时间太长,导致应用程序崩溃?

iphone - 如何将 MonoTouch 应用程序部署到远程设备

iPhone更改按钮点击上的选项卡栏项目

iOS 7 SDK 中的 iOS 模拟器

iphone - 在 IOS-5 中获取 twitter 帐户列表并使用 TWTweetComposeViewController 使用它

ios - UIButton 子类突出显示错误(即使在点击或触摸后突出显示仍然存在)

objective-c - 无法使用 ABNewPersonViewController 在通讯录中显示

ios - 为什么 ion-item click 在 ios 的 ionic4 中不起作用?

iphone - 当项目设置允许所有旋转时,在特定 View Controller 中禁用旋转