Search code examples
iosfunctional-programmingreactive-cocoacancellationphotosframework

Content editing input request as RACSignal


I'm trying to make my code more functional and more reactive. I create a request like this:

[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    PHContentEditingInputRequestOptions *options = [PHContentEditingInputRequestOptions new];
    options.networkAccessAllowed = YES;
    options.progressHandler = ^(double progress, BOOL *stop) {
        // update UI
    };

    [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
        // subscriber send next/completed/error
    }];

    return [RACDisposable disposableWithBlock:^{
        // here I should kill request if still active
    }];
}];

To cancel an iCloud request I have to set *stop = YES; in the progressHandler. How to do this in the reactive way?


Solution

  • I have managed to solve this with NSMutableArray captured in the block.

    [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        // -- Change ---------------------------------------------------
        NSMutableArray *flag = [NSMutableArray array];
        // -- Change End -----------------------------------------------
    
        PHContentEditingInputRequestOptions *options = [PHContentEditingInputRequestOptions new];
        options.networkAccessAllowed = YES;
        options.progressHandler = ^(double progress, BOOL *stop) {
    
            // -- Change ---------------------------------------------------
            if(flag.count > 0) {
                *stop = YES;
            } else {
                // update UI
            }
            // -- Change End -----------------------------------------------
         };
    
        [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
            // subscriber send next/completed/error
        }];
    
        return [RACDisposable disposableWithBlock:^{
            // -- Change ---------------------------------------------------
            [flag addObject:@0];
            // -- Change End -----------------------------------------------
        }];
    }];