延迟、取消或替换 NSOperationQueue 中的 ASIFormDataRequest(对于搜索框)
我使用下面的代码成功创建了 ASIFormDataRequest。
//get groups
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
//make the url by appending the URL from the Constant class to the jsp name
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", URL, @"connectors/searchGroupsServlet.jsp"]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:@"User-Agent" value:USER_AGENT];
[request addPostValue:[login username] forKey:@"username"];
[request addPostValue:[login password] forKey:@"password"];
[request addPostValue:[searchText lowercaseString] forKey:@"query"];
[request addPostValue:GROUP_FILTER_LIMIT forKey:@"limit"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
当前,用户在搜索框中每次按键时都会发出此请求(键入的文本在请求中作为搜索字符串发送)。但是,我不想在每次按键时发送请求,而是希望将请求延迟一秒钟,以允许用户在发送请求之前在搜索框中输入更多字符。
我已经成功创建了一个线程,当用户继续键入时等待一秒钟(尽管我承认,我不相信这是最好的方法,但它现在有效)...
这
[self performSelectorInBackground:@selector(wait:) withObject:request];
调用了这个
-(void)wait:(NSString *)request
{
[NSThread sleepForTimeInterval:1.00];
[[self queue] addOperation:request]; //queue is an NSOperationQueue
}
,但是,如果用户继续键入时,我还没有设法弄清楚如何取消请求或不将请求放入队列中,或清空队列并将其替换为新请求。
最后,显然我可以强迫用户等待,直到他们按下弹出键盘上的“搜索”按钮,但我希望在没有这个的情况下提供搜索结果。
谢谢
I'm successfully making a ASIFormDataRequest using the below code.
//get groups
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
//make the url by appending the URL from the Constant class to the jsp name
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", URL, @"connectors/searchGroupsServlet.jsp"]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:@"User-Agent" value:USER_AGENT];
[request addPostValue:[login username] forKey:@"username"];
[request addPostValue:[login password] forKey:@"password"];
[request addPostValue:[searchText lowercaseString] forKey:@"query"];
[request addPostValue:GROUP_FILTER_LIMIT forKey:@"limit"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
This request is currently made on every key press a user makes in a searchbox (The text typed is sent off in the request as the search string). However, rather than sending the request on every key press, I want to delay the request by a second to allow users to type further characters into the searchbox before the request is sent.
I've successfully made a thread that waits a second as users continue to type (although admittedly Im not convinced this is the best way to do it yet, but it works for now)...
this
[self performSelectorInBackground:@selector(wait:) withObject:request];
calls this
-(void)wait:(NSString *)request
{
[NSThread sleepForTimeInterval:1.00];
[[self queue] addOperation:request]; //queue is an NSOperationQueue
}
but, if a user continues to type, I haven't managed to work out how to cancel the request or not put the request in the queue, or empty the queue and replace it with the new request.
Finally, obviously I could force users to wait until they have pressed the 'search' button on the pop-up keyboard, but I was hoping to provide search results without that.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案是创建一个 NSTimer,并在按下新按键时使其失效。然后重新开始。
The answer was to create an NSTimer, and invalidate it whenever a new key press had been made. Then start it again.
您可以尝试这样取消
You can try this to cancel