问题
I have several Requests that depend on each other and must me called in sequence? Can somebody give me an example using AFNetworking and reactive cocoa?
Example:
- LoginRequest (return transactionId)
- UpdateRequest post data with transactionId
- UploadRequest jpeg with transactionId
- EndRequest with transactionId
回答1:
The method names are clearly made-up but should give you a sense of the form of the code you'd write:
[[self
executeLoginRequest]
flattenMap:^(id transactionId) {
return [[[self
executeUpdateRequest:data withTransactionId:transactionId]
then:^{
return [self executeUploadRequest:jpeg withTransactionId:transactionId];
}]
then:^{
return [self endRequests:transactionId];
}];
}]
We're using -flattenMap:
to take the result of the login request and then make more requests off of it.
来源:https://stackoverflow.com/questions/22100683/how-to-combine-afnetworking-2-0-with-reactive-cocoa-to-chain-a-queue-of-requests