等待响应需要大约300毫秒,所以我每秒只能向我的自定义设备发送3-4个请求,它的速度很慢,我正在努力提高“用户交互”的速度。
现在我正在使用此代码:
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:0.5];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
答案 0 :(得分:1)
您可以异步地正确实施+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
假设您不想每5秒钟发出一组请求。您可以实现发出第一个请求的重复计时器。您可以使请求委托按顺序发出请求(每个委托发出下一个请求)。在非常简化的元语言中,它看起来像:
- repeatingTimer5sHandler {
issue1stRequestWithHandler:handlerFor1stRequest;
}
- handlerFor1stRequest {
handle response
issue2ndRequestWithHandler:handlerFor2ndRequest;
}
- handlerFor2ndRequest {
handle response
issue3rdRequestWithHandler:handlerFor3rdRequest;
}
- handlerFor3rdRequest {
handle response
issue4thRequestWithHandler:handlerFor4thdRequest;
}
- handlerFor4thRequest {
handle response
}
this page的底部是一个有效的解决方案。这只是一个请求,但它应该让你开始。