https://kotlinlang.org logo
#apollo-kotlin
Title
# apollo-kotlin
s

Spencer

02/16/2023, 9:08 PM
Random question, Is there a way to stop
CacheAndNetwork
from emitting 2 values when the cache value and network value are the same?
1
s

Stylianos Gakis

02/16/2023, 9:14 PM
If the items are equal, you could do
.distinctUntilChanged
on the flow itself. Don’t know if there’s something that can be done on the apollo-kotlin side though for this, or if something should be done there.
s

Spencer

02/17/2023, 4:28 PM
That was a good idea, but when I tried it with
Copy code
.distinctUntilChanged { prev, current ->
    prev.data?.equals(current.data)
}
it was always coming up false
s

Stylianos Gakis

02/17/2023, 4:47 PM
So you said that your values are the same and you don’t want to re-emit. Yet the
equals
method returns false for them. So either • They are not actually the same • Their equals method is somehow broken Have you checked why this returns false? Have you debugged and made sure those two objects are in fact the exact same data? Try something like that and if it still doesn’t work we can maybe figure out what’s going wrong. Also as a side note, where are you applying this
distinctUntilChanged
? Showing some more code may help in case there’s some small mistake somewhere
s

Spencer

02/17/2023, 4:51 PM
Yeah I have checked with the debugger and they are the exact same data. Whats really weird is doing
prev.data.toString() == current.data.toString()
makes it return
true
then.
Copy code
apolloClient.query(query)
    .fetchPolicy(CacheAndNetwork)
    .watch()
    .distinctUntilChanged { prev, current ->
        prev.data.toString() == current.data.toString()
    }
    .collect { response ->
        //....
    }
}.catch {
    //....
}
My hope was that if we use CacheAndNetwork, and the response are the exact same, there really isn’t much of a need to rerun the collect again
s

Stylianos Gakis

02/17/2023, 4:53 PM
But in the debugger, doing .equals also returns
false
I suppose right? Even though their
toStrings()
return true. This may mean that their
toString()
is not really printing out everything, and that thing which is not printed out is different between them. Other than that, I can’t imagine how else this could be failing. Remind me, are those generated classes all
data
classes, or do they have their
equals
methods manually generated somehow?
s

Spencer

02/17/2023, 5:01 PM
Correct, even in the debugger doing the equal is false. I just checked that the toString length matches the length of the network response payload from the network profiler. All of the generated classes are Data classes, but I don't see any equal() in the generated apollo types
There must be something weird going on because the hash code is also different 😕
s

Stylianos Gakis

02/17/2023, 5:53 PM
Can you try doing the same but with a smaller object? One that you can check with your own eyes to see if there's something different? This object looks way too big to check manually
Can it be something tricky like a list of items being returned with different ordering even though they're the same size or something silly like that? 😅
s

Spencer

02/17/2023, 5:59 PM
All the time I was doing it, I was taking the raw data from the debugger and sticking it in a online diff checker
🤦 Well I figured it out. I had a custom scalar but it was a class and not a data class. Turning it into a data class fixed it immediately. Thanks for all your help talking through it!!!
s

Stylianos Gakis

02/17/2023, 10:43 PM
Heh so it was something to do with data classes 😄 You could also implement
equals
yourself properly instead of converting it into a data class if you don’t need that. But so glad to help you, very happy that it now works 🎉
21 Views