gpt4 book ai didi

ios - 是否可以在不等待 iOS 响应的情况下发送 GET 请求?

转载 作者:行者123 更新时间:2023-11-29 13:39:10 25 4
gpt4 key购买 nike

等待响应大约需要 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];

最佳答案

你可以异步触发请求,正确实现 + (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的底部是一个可行的解决方案。它仅适用于一个请求,但应该可以帮助您入门。

关于ios - 是否可以在不等待 iOS 响应的情况下发送 GET 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9647706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com