Coder Social home page Coder Social logo

Comments (6)

andersio avatar andersio commented on May 4, 2024

Let's say you have a source producer. One way I can think of is:

SignalProducer(value: source)
    .delay(1.0, on: UIScheduler())
    .times(Int.max)
    .flatten(.latest)
    .replayLazily(upTo: 1)

from reactiveswift.

mfclarke avatar mfclarke commented on May 4, 2024

I knew there was a nice way to do it. Thanks @andersio

from reactiveswift.

andersio avatar andersio commented on May 4, 2024

@mfclarke Hmm, after a second thought, the replayLazily is not in the right place, causing repeated work instead of the intended caching behavior. See the updated snippet. Sorry about the mistake.

Note that it works fine only if one value is cached.

from reactiveswift.

mfclarke avatar mfclarke commented on May 4, 2024

Actually, unfortunately neither solution works (the first one or the updated one). To check it, I'm doing this (with DataFetcher being a mock that takes 1 second to return dummy data):

let lazyWithCacheExpiry = SignalProducer(value: DataFetcher.fetch())
    .delay(1.0, on: QueueScheduler.main)
    .times(Int.max)
    .flatten(.latest)
    .replayLazily(upTo: 1)

print("Starting producer 3 times - all 3 logs should happen once after 1s")
lazyWithCacheExpiry.startWithValues { data in
    print("1st returned: \(data)")
}

lazyWithCacheExpiry.startWithValues { data in
    print("2nd returned: \(data)")
}

lazyWithCacheExpiry.startWithValues { data in
    print("3rd returned: \(data)")
}

DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
    print("-------------")
    print("Get data after cache expiry - should do second fetch")
    lazyWithCacheExpiry.startWithValues { data in
        print("Returned: \(data)")
    }
}

I also have a print in my mock fetcher to verify it only fetches once.

I get this output:

Starting producer 3 times

Then this repeats a random number of times, from DataFetcher.fetch (from 2 to ~10):

Performing fetch

Eventually we get:

1st returned: ["a": 1]
2nd returned: ["a": 1]
3rd returned: ["a": 1]

Which will then repeat every 1s.

Always 10s after running this appears:

-------------
Get data after cache expiry - should do second fetch

Immediately followed by:

Returned: ["a": 1]

What I think I need after considering your approach is 2 producers. One doing the work with replayLazily assigned to one var and another that is started when the owner object is created which replaces the replayLazily producer every n seconds using delay and times. The downside to this approach though is if the replayLazily var is used in another context, for example with a .on. This will never get replaced, since it's already been copied.

from reactiveswift.

andersio avatar andersio commented on May 4, 2024

Yeah, it does not invalidate the cached value upon expiry, and have subscriptions after the expiry to wait for the new value. It just starts a request upon expiry so that eventually the cached value is updated.

Maybe it is worth to have something like replayLazily(upTo:timeout:).

from reactiveswift.

mfclarke avatar mfclarke commented on May 4, 2024

Ahh I see. That would be kind of cool to have a replayLazily(upTo:timeout:). I'd be more than happy to code it up if it's something you guys would like to have? I might need a little bit of hand holding because I haven't been using RAC that long, but would love to have a shot at it!

Here's where I've landed with the issue though:

class DataProvider {

    var producer = DataProvider.buildProducer()
    private var replacerDisposable: Disposable?

    init(withExpiryInterval expiryInterval: TimeInterval) {
        replacerDisposable = QueueScheduler.main.schedule(after: Date(), interval: expiryInterval) { [weak self] in
            self?.producer = DataProvider.buildProducer()
        }
    }

    private static func buildProducer() -> SignalProducer<MyData, MyFetchError> {
        return DataFetcher.fetch().replayLazily(upTo: 1)
    }

}

It's neat enough for my purposes, and doesn't leak state so that's a plus.

from reactiveswift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.