简体   繁体   English

避免在ReactiveCocoa上重复请求http

[英]Avoid repeated http requests on ReactiveCocoa

I have one signal that basically what it does is requesting for a configuration using NSRULSession. 我有一个信号,它基本上是在要求使用NSRULSession进行配置。 When I do a subscribeNext it does the request perfectly fine, however for the second time this request is not necessary anymore. 当我执行一个subscribeNext时,它会很好地处理请求,但是第二次不再需要此请求。 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. 听起来您想要的是replayLast运算符。 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/ 在此处阅读“重播”运算符: http : //spin.atomicobject.com/2014/06/29/replay-replaylast-replaylazily/

One time signal could be made via take: operator. 可以通过take:运算符发出一次时间信号。 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. 在执行了如此多的操作之后,该网关将完全关闭,并且没有更多数据将在subscribeNext:块中传递。 In your case this amount would be equal 1. 在您的情况下,该金额等于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. 然后仅在需要时触发操作以刷新属性的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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