Ok last one for caching I swear :sweat_smile: :thr...
# apollo-kotlin
m
Ok last one for caching I swear 😅 🧵
😅 2
Now that all the entries looks correct I'm trying to use apollo as the source of truth. So I hook up a watcher :
Copy code
fun workOrder(id: String): Flow<Pair<WorkOrderInfo, List<FetchWorkOrderQuery.ValidFieldsetOption>>> {
        return apolloClient.query(FetchWorkOrderQuery(id = id))
            .fetchPolicy(FetchPolicy.CacheFirst)
            .watch()
            .map {
            ...
            }
    }
And the Have A mutation with an optmistic cache:
Copy code
apolloClient.mutation(UpdateTaskComponentsMutation(input = newComponents))
            .doNotStore(false)
            .optimisticUpdates(
                UpdateTaskComponentsMutation.Data(
                    UpdateTaskComponentsMutation.UpdateTaskComponents(
                        listOf(
                            UpdateTaskComponentsMutation.Component(
                                __typename = newComponent.componentInfo.__typename,
                                componentInfo = newComponent.componentInfo,
                                id = newComponent.componentInfo.id
                            )
                        )
                    )
                )
            )
            .execute()
This seems to be writing in the cache alright, ( If I re enter the screen I can see the new value) But for some reason the watch is not emitting?
https://www.apollographql.com/docs/kotlin/caching/query-watchers Could not fin much more here, im goign over video sin youtube to see if I find something
m
Is your new component id the same as the one from the initial query?
m
Yep double checked and its the same ( also checked the db and the entry is corretly updated)
m
You can try make sure your watcher Flow is not cancelled or terminated (maybe add a
onCompletion { println("complete) }
)
Also worth trying to publishing one of the cache keys manually to see if it triggers the watcher (
apolloClient.apolloStore.publish(setOf("Component:42"))
)
m
sure thing, will check
m
If nothing works, good old breakpoint debugging should give more information. The interesting bits are here
watch()
also takes 2 parameters (I'm assuming you're using v3, right?). You can try setting them to true to make the flow throw on cache misses
m
Cool, publishing did not work either. Will cehck the params and also debug
thanks!
Ok interestingly enough. After some debug ( thanks for pointing into the right place). It seems I was adding way to many watched keys. I stoped watching one of the flows and now works. Maybe the huge amount was causing some crash that was swallowed
m
Interesting 🤔 Given the amount of RAM that devices have these days, feels like this should work nonetheless
m
Im talking about 4k keys ( if the debuggers was right 😛 )
m
That's like nothing for 2GHz CPU with several GB of RAM
What's interesting is that the other flow you were watching is not related to the 1st one?
m
oh thats good to know, maybe Il spend soem more time at udnerstanding as I was a bit worried that we reach a big number anyways
What's interesting is that the other flow you were watching is not related to the 1st one?
But they all add watch keys to the pool right?
m
Copy code
watchedKeys == null || changedKeys.intersect(watchedKeys!!).isNotEmpty()
watchedKeys
here is local to one query
m
👀
m
changedKeys
is whatever changed in the store
m
right, I know a bit better to where look now
cheers!
🍻 1