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];