Siesta-Swift: Is there a way to find out if Resource.latestData has changed?

92 views Asked by At

My code looks like the following:

let myAPI = Service(baseURL: "...")
myAPI.resource("/...").addObserver(owner: self) { resource, event in
}

if the URL always returns 200 (NOT 301), is there a way to check resource or event if the data itself, the json-content, changed or if it's the same?

I would like to achieve a behavior where the app does the loading in the background and only triggers a UI-action if the JSON itself changed.

1

There are 1 answers

0
Paul Cantrell On

Siesta currently does not support this. It can’t automatically do so in the general case, since there is no universally valid definition of “changed” for every type of resource content. (Even if the resource content is Equatable, and that happens to match the definition of “changed” you want, should some headers should be included in the notion of equality? Which ones?)

The 304 is a nice bright line: the server, arbiter of truth for the resource, declares it semantically unchanged — and all the API-specific rules about what “unchanged” means live on the server.

This does, however, seem like a nice thing to be able to customize. I recommend filing an issue if you’d like to see it implemented.

In the meantime, you can do it by hand: make your resource payload conform to Equatable and check it in your resource observers. If you have performance-sensitive updates, an approach with this shape sometimes makes more sense anyway, since you can check for partial changes (e.g. only some elems in a collection changed).