LoginSignup
24
23

More than 5 years have passed since last update.

逆引きReactiveCocoa: UITextFieldに入力された値をインクリメンタルサーチする

Posted at

テキストフィールドに入力された値からインクリメンタルサーチを行いたい場合というのはよくある要件ではないでしょうか。そのようなパターンを ReactiveCocoa では以下のように実装することができます。

[[[[[textField.rac_textSignal
    filter:^BOOL(NSString *text) {
        return text.length > 0;
    }]
    throttle:0.5]
    map:^(NSString *text) {
        return [[APIClient sharedClient] fetchSearchResultWithQuery:text];
    }]
    switchToLatest]
    subscribeNext:^(id JSON) {
        NSLog(@"Search result: %@", JSON);
    }];
  1. -filter:で空文字を除外する。
  2. -throttle:で、入力が一定時間止まった場合にだけテキストの変化を検知するようにする。
  3. -map:で実際に検索処理を実施・結果を通知するRACSignalを返す(これは適宜実装)。
  4. -switchToLatestで、検索処理が複数回行われる場合に直前の検索処理をキャンセルする。

という流れになります。

24
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23