Avoid repeated http requests on ReactiveCocoa

170 views Asked by At

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?

3

There are 3 answers

1
skyylex On

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")
}]
0
powerj1984 On

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/

0
aehlke On

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.