2

我有一堆批量发送的 URL 请求,然后是一个接收结果的“捕手”类。我通过 facebook 的图形 API SDK 执行此操作,因此我无法控制实际的 NSURL。我的问题是,在所有请求都回来之前,我不会触发下一个结果协议,但有时,尤其是在 3G 上,它们并不总是回来(尤其是当你有几百个请求时)。什么是“放弃”给定请求的好方法?对于那些不熟悉 FB api 的人来说,实际请求的工作方式是初始化一个 facebook 对象,然后从中请求数据,然后指定一个返回委托。因此,对于每个单独的请求,我都有一个 returnDelegate(捕手),我可以针对每个请求进行初始化。

更新:来自 FB-API 的一些代码

- (FBRequest*)requestWithMethodName:(NSString *)methodName
                          andParams:(NSMutableDictionary *)params
                      andHttpMethod:(NSString *)httpMethod
                        andDelegate:(id <FBRequestDelegate>)delegate {
    NSString * fullURL = [kRestserverBaseURL stringByAppendingString:methodName];
    return [self openUrl:fullURL
                  params:params
              httpMethod:httpMethod
                delegate:delegate];
}

- (FBRequest*)openUrl:(NSString *)url
               params:(NSMutableDictionary *)params
           httpMethod:(NSString *)httpMethod
             delegate:(id<FBRequestDelegate>)delegate {

    [params setValue:@"json" forKey:@"format"];
    [params setValue:kSDK forKey:@"sdk"];
    [params setValue:kSDKVersion forKey:@"sdk_version"];
    if ([self isSessionValid]) {
        [params setValue:self.accessToken forKey:@"access_token"];
    }

    [self extendAccessTokenIfNeeded];

    FBRequest* _request = [FBRequest getRequestWithParams:params
                                               httpMethod:httpMethod
                                                 delegate:delegate
                                               requestURL:url];
    [_requests addObject:_request];
    [_request addObserver:self forKeyPath:requestFinishedKeyPath options:0 context:finishedContext];
    [_request connect];
    return _request;
}

- (void)connect {

    if ([_delegate respondsToSelector:@selector(requestLoading:)]) {
        [_delegate requestLoading:self];
    }

    NSString* url = [[self class] serializeURL:_url params:_params httpMethod:_httpMethod];
    NSMutableURLRequest* request =
    [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                        timeoutInterval:kTimeoutInterval];
    [request setValue:kUserAgent forHTTPHeaderField:@"User-Agent"];


    [request setHTTPMethod:self.httpMethod];
    if ([self.httpMethod isEqualToString: @"POST"]) {
        NSString* contentType = [NSString
                             stringWithFormat:@"multipart/form-data; boundary=%@", kStringBoundary];
        [request setValue:contentType forHTTPHeaderField:@"Content-Type"];

        [request setHTTPBody:[self generatePostBody]];
    }

    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.state = kFBRequestStateLoading;
    self.sessionDidExpire = NO;
}
4

1 回答 1

2

如上所述,您可以通过实例化一个在经过一定时间时向委托对象发出信号的方法来实现您自己的超时。NSTimer

于 2012-04-21T15:53:35.723 回答