Waiting for request to be processed [duplicate]

2019-06-14 08:20发布

This question already has an answer here:

I was wondering if I could wait for request to be processed with afnetworking.

Lets say I got this method

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

Is that doable?

4条回答
不美不萌又怎样
2楼-- · 2019-06-14 08:49

You can, but you never want the main queue waiting for some asynchronous operation to complete. If you want something to happen after your asynchronous operation is done, you should use the AFNetworking success block to specify what you want to happen when the operation is done.

So, if you want to provide the caller a pointer to the MWPhoto, rather than having a return type of MWPhoto *, have a return type of void, but supply a completion block so that the caller can handle it when it's done:

- (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
    }
}

So, rather than:

MWPhoto *photo = [object photoBrowser:photoBrowser photoAtIndex:index];
// do whatever you want with `photo` here

You might instead do:

[object photoBrowser:photoBrowser photoAtIndex:index completion:^(MWPhoto *photo){
    // do whatever you want with `photo` here
}];
查看更多
We Are One
3楼-- · 2019-06-14 08:49

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;
}
查看更多
SAY GOODBYE
4楼-- · 2019-06-14 08:52

This would be referred to a synchronous request.

If the method is called on the main thread it will make your app appear to have frozen and is not a suggested way to do networking.

See the dupe question I commented for details on how to do it if you still want to.

查看更多
等我变得足够好
5楼-- · 2019-06-14 08:54

Since AFURLConnectionOperation inherits from NSOperation, you can use NSOperation waitUntilFinished method to wait for the operation to end.

However, the success and failure blocks of AFURLConnectionOperation will be executed before waitUntilFinished completes. Nevertheless, you can access the response and error properties of the AFURLConnectionOperation after waitUntilFinished completes.

查看更多
登录 后发表回答