Managing a bunch of NSOperation with dependencies

后端 未结 4 1408
面向向阳花
面向向阳花 2021-02-01 08:40

I\'m working on an application that create contents and send it to an existing backend. Content is a title, a picture and location. Nothing fancy.

The backend is a bit c

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 09:12

    I'm probably totally, biased - but for a particular reason - I like @Rob's approach #6 ;)

    Assuming you created appropriate wrappers for your asynchronous methods and operations which return a Promise instead of signaling the completion with a completion block, the solution looks like this:

    RXPromise* finalResult = [RXPromise all:@[[self filename], [self process]]]
    .then(^id(id filenameAndProcessResult){
        return [self generateEntry];
    }, nil)
    .then(^id(id generateEntryResult){
        return [self uploadImage];
    }, nil)
    .thenOn(dispatch_get_main_queue() , ^id(id uploadImageResult){
        [self refreshWithResult:uploadImageResult];
        return nil;
    }, nil)
    .then(nil, ^id(NSError*error){
        // Something went wrong in any of the operations. Log the error:
        NSLog(@"Error: %@", error);
    });
    

    And, if you want to cancel the whole asynchronous sequence at any tine, anywhere and no matter how far it has been proceeded:

    [finalResult.root cancel];
    

    (A small note: property root is not yet available in the current version of RXPromise, but its basically very simple to implement).

提交回复
热议问题