Reputation: 19148
In my app i use ReactiveCocoa and the AFNetworking-Extension for API-Calls. For each API-Endpoint i have a method starting the API-Request and returning the RACSignal.
With the results of the API-Calls i populate mostly UITableViews.
There are multiple events, which result in Reloading/Refreshing the View per API-Request:
How can i prevent, that new API-Requests will be executed before an currently executing request to the same endpoint has finished?
I know, that i can use "throttle" to throttle user-inputs like tap-events. But as already mentioned there are several occasions which can start a new API-Request.
I could work with flags, which are set when starting the request and will be resetted in "completed"-block.
But are there built-in methods in ReactiveCocoa to use instead of?
Upvotes: 2
Views: 626
Reputation: 3063
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)
Upvotes: 2