1

I have several requests that I need to chain. I can do it like this

provider.request(.apples) { (result) in
        switch result {
        case .success(let response):

           provider.request(.oranges) { (result) in
                switch result {
                case .success(let response):

                   ...couple of other fruits

                case .failure(let error):
                  print(error)
        }
    }

        case .failure(let error):
            print(error)
        }
    }

But I want to chain them using NSOperation.

How can I do this ?

Alexey K
  • 6,537
  • 18
  • 60
  • 118

1 Answers1

-1

I am a beginner in Swift ,but going thro the code I understand that your are making a request for "apples". if that request is successful, then making another request for "Oranges", and if that is successful, requesting other fruits, and so on.

Yes, you can chain them using NSOperation, please refer to my answers for the below question, where I have outlined 2 different approaches.

Do NSOperations and their completionBlocks run concurrently?

Note: There are a few scenarios here: 1) Do you need to request "oranges" only if "apples" request succeeded, and request other fruits only if "oranges: request succeeds? In that case, you can refer to the answers in the above mentioned question. Sample flow below:

RequestFruitOperation *requestApplesOperation = [[RequestFruitOperation alloc] initWithFruitType:Apples];

[requestApplesOperation setCompletionBlock:^{
    if(requestApplesOperation.success){
        //add oranges
        RequestFruitOperation *requestOrangesOperation = [[RequestFruitOperation alloc] initWithFruitType:Oranges];
        [requestOrangesOperation setCompletionBlock:^{
            if(requestOrangesOperation.success) {
                //add mangoes
                RequestFruitOperation *requestMangosOperation = [[RequestFruitOperation alloc] initWithFruitType:Mangos];
                [operationQueue addOperation:requestMangosOperation];
            }
        }];
        [operationQueue addOperation:requestOrangesOperation];
    }

}
 [operationQueue addOperation:requestApplesOperation];

2) If can you request "apples", "oranges" and "other fruits" in parallel, without waiting for one another to be successful, then you don't need to chain them. You can just add the operations the queue.

RequestFruitOperation *requestApplesOperation = [[RequestFruitOperation alloc] initWithFruitType:Apples];
[operationQueue addOperation:requestApplesOperation];

RequestFruitOperation *requestOrangesOperation = [[RequestFruitOperation alloc] Oranges];
[operationQueue addOperation:requestOrangesOperation];

RequestFruitOperation *requestMangosOperation = [[RequestFruitOperation alloc] Mangos];
[operationQueue addOperation:requestMangosOperation];
Kabeer
  • 84
  • 5
  • Also have a look into this Swift framework which provides advanced features to NSOperation and NSOperationQueue. : https://operations.readme.io/docs/conditions – Kabeer Jul 05 '18 at 03:51