等待请求被处理

我想知道是否可以等待请求通过networking处理。

可以说我有这个方法

- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index { //Request goes here, so the method doesn't return anything before it's processed } 

那可行吗?

这将被称为同步请求。

如果在主线程中调用该方法,则会使您的应用程序显示为冻结,而不是build议的联网方式。

如果你还想要,请参阅我评论的关于如何做的细节。

你可以,但是你永远不希望主队列等待一些asynchronous操作来完成。 如果在asynchronous操作完成之后想要发生某些事情,则应使用AFNetworking success块指定操作完成后要执行的操作。

因此,如果要为调用者提供一个指向MWPhoto的指针,而不是返回types为MWPhoto *MWPhoto * ,则返回types为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 }]; 

由于AFURLConnectionOperation从NSOperationinheritance,所以可以使用NSOperation的waitUntilFinished方法等待操作结束。

但是,AFULConnectionOperation的成功和失败块将在waitUntilFinished完成之前执行。 不过,您可以在waitUntilFinished完成后访问AFURLConnectionOperation的响应和错误属性。

这正是我所做的,这是受到启动同步请求

 - (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index { 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]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(!error) { NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; GalleryPicture *picture = [[GalleryPicture alloc] initWithJSON:json]; return [picture mwPhoto]; } } return nil; }