延迟、取消或替换 NSOperationQueue 中的 ASIFormDataRequest(对于搜索框)

发布于 2024-11-02 13:39:19 字数 1554 浏览 1 评论 0原文

我使用下面的代码成功创建了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2024-11-09 13:39:19

答案是创建一个 NSTimer,并在按下新按键时使其失效。然后重新开始。

[timer invalidate];

The answer was to create an NSTimer, and invalidate it whenever a new key press had been made. Then start it again.

[timer invalidate];
2024-11-09 13:39:19

您可以尝试这样取消

+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument

You can try this to cancel

+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文