简体   繁体   中英

Avoid repeated http requests on ReactiveCocoa

I have one signal that basically what it does is requesting for a configuration using NSRULSession. When I do a subscribeNext it does the request perfectly fine, however for the second time this request is not necessary anymore. How could I avoid it?

Your signal will do its work each time it is subscribed to unless you do something explicit to prevent that. It sounds like what you want here is the replayLast operator. This operator will cache the last emitted value of your signal and emit it when your signal is subscribed to again instead of redoing the initial work.

Read up on the 'replay' operators here: http://spin.atomicobject.com/2014/06/29/replay-replaylast-replaylazily/

One time signal could be made via take: operator. You just need to pass an argument for the amount of times required to perform a signal. After such amount of executions this gateway will be closed completely and no more data will be passed in the subscribeNext: block. In your case this amount would be equal 1.

RACSignal *requestConfigurationSignal = ...
[[requestSignal 
   take:1] 
   subscribeNext:^(id value){
   NSLog(@"Request in progress")
}]

Use a property and an action whose values are bound to that property. Then trigger the action only as needed to refresh the property's value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM