Content editing input request as RACSignal

113 views Asked by At

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?

1

There are 1 answers

0
Tomasz Bąk On BEST ANSWER

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