我想知道是否可以等待afnetworking处理请求。
让我说我得到了这个方法
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
//Request goes here, so the method doesn't return anything before it's processed
}
可行吗?
答案 0 :(得分:1)
这将被称为同步请求。
如果在主线程上调用该方法,它将使您的应用程序看起来已冻结,并且不是建议的联网方式。
如果您仍然想要,请参阅我评论的重复问题,了解如何执行此操作的详细信息。
答案 1 :(得分:1)
您可以,但您永远不希望主队列等待某些异步操作完成。如果您希望在异步操作完成后发生某些事情,则应使用AFNetworking success
块来指定操作完成时您希望发生的事情。
因此,如果您想为调用者提供指向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
}];
答案 2 :(得分:0)
由于AFURLConnectionOperation继承自NSOperation,您可以使用NSOperation waitUntilFinished方法等待操作结束。
但是,在waitUntilFinished完成之前,将执行AFURLConnectionOperation的成功和失败块。但是,您可以在waitUntilFinished完成后访问AFURLConnectionOperation的响应和错误属性。
答案 3 :(得分:0)
这正是我所做的,它指的是启动同步请求
- (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;
}