可能ですが、何らかの非同期操作が完了するまでメイン キューを待機させたくありません。非同期操作が完了した後に何かを発生させたい場合は、AFNetworkingsuccess
ブロックを使用して、操作が完了したときに発生させたいことを指定する必要があります。
したがって、呼び出し元に へのポインターを提供する場合は、戻り値の型を にするのMWPhoto
ではなく、戻り値の型をMWPhoto *
にしますvoid
が、終了時に呼び出し元がそれを処理できるように完了ブロックを指定します。
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index completion:(void (^)(MWPhoto *))completion
{
if (index < self.images.count) {
GalleryPicture *thumbnail = [images objectAtIndex:index];
NSURLResponse *response = nil;
NSError *error = nil;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", API_URL, @"galleryPicture"]];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:gallery.objectId, @"galleryId", thumbnail.objectId, @"id", [NSNumber numberWithBool:NO], @"thumbnail", nil];
ViveHttpClient *httpClient = [[ViveHttpClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFFormURLParameterEncoding;
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:[url path] parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
GalleryPicture *picture = [[GalleryPicture alloc] initWithJSON:JSON];
completion([picture mwPhoto]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
// handle the error here
}];
// start your operation here
}
}
したがって、ではなく:
MWPhoto *photo = [object photoBrowser:photoBrowser photoAtIndex:index];
// do whatever you want with `photo` here
代わりに次のようにすることもできます:
[object photoBrowser:photoBrowser photoAtIndex:index completion:^(MWPhoto *photo){
// do whatever you want with `photo` here
}];