8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

逆引きReactiveCocoa: 副作用だけのシグナルをリフティング/バインディングに使用する

Posted at

元ネタ: Best practice for signals that capture completed/error operations · Issue #653 · ReactiveCocoa/ReactiveCocoa


ログインやデータの永続化など、特に next として送る値がないシグナルの結果を -[NSObject rac_liftSelector:withSignals:]RAC() マクロでのバインディングに使用したい場合にどうするべきかというパターンです。

RACSignal *login = [[[self 
    // ログイン成功時には `completed`, 失敗時には `error` を送る。
    loginWithUserName:userName password:password] 
    concat:[RACSignal return:@YES]]
    catchTo:[RACSignal return:@NO]];

RAC(self, loggedIn) = login;
// or
[self rac_liftSelector:@selector(loginDidSuccess:) withSignals:login, nil];

とすると、-loginWithUserName:password: の実装側では、next として @YES/@NORACUnit.defaultUnit を送る必要がなくなります。

補足

元ネタでは [RACSignal return:@YES]; を返す部分に -then: を使用していましたが、そう遠くない内にリリースされるであろう ReactiveCocoa 3.0 では -then:非推奨 (deprecated) になりそうなので置き換えています。

8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?