Search code examples
iosafnetworking-2reactive-cocoa

How-to combine AFNetworking 2.0 with reactive cocoa to chain a queue of requests together?


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:

  1. LoginRequest (return transactionId)
  2. UpdateRequest post data with transactionId
  3. UploadRequest jpeg with transactionId
  4. EndRequest with transactionId

Solution

  • 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.