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:)];

現在、このリクエストは、ユーザーが検索ボックスでキーを押すたびに行われます (入力されたテキストは、検索文字列としてリクエストで送信されます)。ただし、キーを押すたびにリクエストを送信するのではなく、リクエストが送信される前にユーザーが検索ボックスにさらに文字を入力できるように、リクエストを 1 秒遅らせたいと考えています。

ユーザーが入力を続ける間、1 秒間待機するスレッドの作成に成功しました (確かに、これが最善の方法であるとはまだ確信していませんが、今のところは機能します)...

これ

[self performSelectorInBackground:@selector(wait:) withObject:request];

これを呼び出します

-(void)wait:(NSString *)request
{
    [NSThread sleepForTimeInterval:1.00];
    [[self queue] addOperation:request]; //queue is an NSOperationQueue
}

しかし、ユーザーが入力を続けた場合、リクエストをキャンセルする方法、リクエストをキューに入れないようにする方法、またはキューを空にして新しいリクエストに置き換える方法を見つけることができませんでした。

最後に、ユーザーがポップアップ キーボードの [検索] ボタンを押すまで待つように強制することもできますが、それなしで検索結果を提供したいと考えていました。

ありがとう

4

2 に答える 2

1

答えは、NSTimerを作成し、新しいキーが押されるたびにそれを無効にすることでした。その後、もう一度開始します。

[timer invalidate];
于 2011-04-26T09:05:11.920 に答える
0

これを試してキャンセルできます

+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument
于 2011-04-26T09:06:57.477 に答える