Can I get Siesta to look at my persistent cache before making a network request?

180 views Asked by At

I've started using Siesta while running in the background, using Apple's background fetch capabilities. One of the (many) difficult things to work with while running this way is that on some devices, the OS tends to kill my process frequently. I am trying to get my processing to be fast and as battery efficient as possible, so that the OS will choose to run it regularly.

As I understand it, if Siesta has no data in its in-memory cache (which is the case if the app is newly launched), then it makes both a network request and a persistent cache request. I often have perfectly good, non-stale data in the persistent cache in this scenario. Can I get Siesta to pre-load that data into the in-memory cache, before it makes the network request? Then my code uses less battery, it gets run regularly, every thing is great!

1

There are 1 answers

0
Paul Cantrell On

As it happens, I just hit this issue myself working on a prepackaged FileCache implementation for Siesta. I think it’s fair to call it a bug.

When you first bring a Resource into memory, Siesta fires off the cache check asynchronously. That’s as it should be — we don’t want expensive data loading to hold up the UI thread — but loadIfNeeded() doesn’t wait for the cache check to complete.

An improved design would have loadIfNeeded() still return a Request if there’s no data yet, but have that request be a chained one that first checks the cache and then checks the network.

In the meantime, a (very ugly) workaround for this is to delay your loadIfNeeded() call:

let resource = service.resource("whatever")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
  resource.loadIfNeeded()
}

Update: I’ve filed an issue for this.