在我的应用程序中,我使用ReactiveCocoa和用于API调用的AFNetworking-Extension。 对于每个API-Endpoint,我有一个启动API-Request并返回RACSignal的方法。
使用API-Calls的结果我主要填充UITableViews。
有多个事件,导致重新加载/刷新每个API请求的视图:
如何防止在对同一端点的当前执行请求完成之前执行新的API请求?
我知道,我可以使用"油门"限制用户输入,如点击事件。但正如已经提到的,有几种情况可以启动新的API请求。
我可以使用标志,这些标志在启动请求时设置,并将在"已完成" -block中重置。
但ReactiveCocoa中是否有内置方法而不是?
答案 0 :(得分:2)
As @Jakub-Vanu mention RACCommand is your friend here.
You can have a RACCommand that returns a signal that sends the results of the network request as Next events.
let apiFetchCommand: RACCommand = RACCommand(signalBlock: { [weak self](object: AnyObject!) -> RACSignal! in
return self.fetchSignal()
})
Then if you want a signal to that sends the next events that come from that commmand you can user the executionSignals
property on RACCommand.
let fetchedResultsSignal: RACSignal = self.apiFetchCommand.executionSignals.switchToLatest()
This signal can be used to listen to the results from that command.
RACCommands have a property called allowsConcurrentExecution
which is defaulted to false. This means that the command will not fire if the signal it returns has not yet completed.
Then to execute the command just call
self.apiFetchCommand.execute(someObject)