懒惰的`SignalProducer`,在消耗所有数据时异步获取更多数据

时间:2016-04-05 20:56:46

标签: frp reactive-cocoa-4

让我们想象一下,我们可以异步获取固定数量的消息(一个请求,包含N个元素)

func fetchMessages(max: UInt, from: Offset) ->  SignalProducer<Message,NoError> 

现在,我想把它变成一个无限制的SignalProducer,当前一个流完成时,它会懒惰地调用fetchMessages

func stream(from: Offset) -> SignalProducer<Message, NoError> {
    // challenge is to implement this function
}

一个可行的初步想法,但仍然需要预先计算所有范围将是以下代码的泛化

func lazyFetchFrom(from: Offset) -> SignalProducer<Message,NoError> {
        return SignalProducer<Message,NoError> { (observer, disposable) in
            fetchMessages(from).start(observer)
        }
    }

    let lazyStream =
        fetchMessages(1000, from)
            .concat(lazyFetchFrom(from + 1000))
            .concat(lazyFetchFrom(from + 2000))
            .... // could probably be done generically using a flatMap

现在,我想更进一步,一旦消耗了之前的值,就评估下一次对lazyFetchFrom的调用。这可能吗?

由于

PS:很明显,我主要担心的是提供某种背压,这样生产者与消费者相比生产速度不会太快

编辑:here is我最近尝试实施一些背压。然而,当我们观察到信号时,背压消失,所有内容都在内存中排队

1 个答案:

答案 0 :(得分:0)

- (RACSignal *)allContentFromId:(NSInteger)contentId afterDate:(NSDate *)date fetchSize:(NSInteger)fetchSize {
    RACSignal *signalNextPagination = [self nextPaginationAllContentFromId:contentId afterDate:date fetchSize:fetchSize];

    //in signalNextPagination will be send next fetch size data and send complete only after downloaded all data
    //so we used aggregate

    return [signalNextPagination aggregateWithStart:@[] reduce:^id(NSArray *before, NSArray *next) {
        return [before arrayByAddingObjectsFromArray:next];
    }];
}


- (RACSignal *)nextPaginationAllContentFromId:(NSInteger)contentId afterDate:(NSDate *)date fetchSize:(NSInteger)fetchSize {

    //command will be send one fetch request
    //after recv data in command need try size fetch
    //if size eq fetch size, so need repeat command with new offset

    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDate *date) {
        return [self requestContentFromId:contentId afterDate:date limit:fetchSize];
    }];
    command.allowsConcurrentExecution = YES;

    RACSignal *download = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[command.executionSignals flattenMap:^RACStream *(id value) {
            return value;
        }] subscribeNext:^(NSArray *datas) {
            [subscriber sendNext:datas];
            if ([datas count] == fetchSize) {
                NSDate *date = [[datas firstObject] pubDate];
                [command execute:date];
            } else {
                [subscriber sendCompleted];
            }
        } error:^(NSError *error) {
            [subscriber sendError:error];
            [subscriber sendCompleted];
        }];

        [command execute:date];
        return nil;
    }];

    return download;
}