0

我想知道我是否可以等待使用afnetworking处理请求。

可以说我有这个方法

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

那可行吗?

4

4 回答 4

1

这将被称为同步请求。

如果在主线程上调用该方法,它将使您的应用程序看起来已冻结,并且不是建议的联网方式。

如果您仍然愿意,请参阅我评论的欺骗问题以获取有关如何执行此操作的详细信息。

于 2013-09-18T19:02:12.853 回答
1

你可以,但你永远不希望主队列等待一些异步操作完成。如果您希望在异步操作完成后发生某些事情,您应该使用 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
}];
于 2013-09-18T19:04:53.787 回答
0

This is exactly what I did, which reffers to starting synchronyous request

- (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;
}
于 2013-09-18T19:14:00.223 回答
0

由于 AFURLConnectionOperation 继承自 NSOperation,您可以使用 NSOperation waitUntilFinished 方法等待操作结束。

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

于 2013-09-18T19:03:49.133 回答