Comments (13)
Here is a quick snippet. If you need more details let me know, then I will try to write an expo snack when I got some more time 🤗
import React, { Component } from 'react'
import { persistCachePromise, apolloClient } from './apollo-client-setup'
import { ApolloProvider } from 'react-apollo'
// App is your react-native root component
// we delay any app rendering until the store is hydrated
export class App extends Component {
constructor(...args) {
super(...args)
this.state = { isHydratingStore: true }
}
componentDidMount() {
persistCachePromise.then(() => {
this.setState({ isHydratingStore: false })
})
}
render() {
if (this.state.isHydratingStore) {
return null
}
return (
<ApolloProvider client={apolloClient}>
<YourRealApp />
</ApolloProvider>
)
}
}
from apollo-cache-persist.
Update : I realize that the problem maybe not comes from apollo-cache-persist but from apollo client. Because the problem only happens on iOS and not on Android.
Considering the following snippet :
render() {
if (this.props.queryUserLoggedInfos.networkStatus === 1) return null;
const { userLogged } = this.props.queryUserLoggedInfos;
console.log("=========render userLogged==========");
console.log(userLogged);
console.log("====================================");
(...)
I got the following result on iOS :
[apollo-cache-persist] Restored cache of size 90659
=========render userLogged==========
{id: "5a2c769d4ef79d57e29c940a", pseudo: "sof", email: "[email protected]", avatar: "ws5icnrjntmd4bbbvlay", tribu: {…}, …}
====================================
=========render userLogged==========
{id: "5a2c769d4ef79d57e29c940a", pseudo: "sof", email: "[email protected]", avatar: "ws5icnrjntmd4bbbvlay", tribu: {…}, …}
====================================
remote-redux-devtools: Socket connection errors are being suppressed.
This can be disabled by setting suppressConnectErrors to 'false'.
SocketProtocolError {name: "SocketProtocolError", message: "Socket hung up", code: 1006, stack: "SocketProtocolError: Socket hung up↵ at SCSocke…a/node_modules/expo/tools/hashAssetFiles:2316:42)"}
=========render userLogged==========
null
====================================
[apollo-cache-persist] Persisted cache of size 90595
So after the first two renders, the cache is filled but at the end of the third it becomes empty. And this problem does not appear on Android...
On Android there only are two renders instead of three.
[apollo-cache-persist] Restored cache of size 35420
=========render userLogged==========
Object {
"__typename": "User",
"avatar": "muv3zrworf6bg2mcap9o",
"email": "[email protected]",
(...)
}
====================================
=========render userLogged==========
Object {
"__typename": "User",
"avatar": "muv3zrworf6bg2mcap9o",
"email": "[email protected]",
(...)
},
}
====================================
[apollo-cache-persist] Persisted cache of size 33281
Really strange behavior...
from apollo-cache-persist.
I can confirm this issue on iOS version 10. If I add a console.log in the render method of my component it will log three times. The first two times are with the actual data object and the last on with null
.
I am using the following:
"apollo-cache-inmemory": "1.1.7",
"apollo-cache-persist": "0.1.1",
"apollo-client": "2.2.3",
"apollo-link": "1.1.0",
"apollo-link-context": "1.0.5",
"apollo-link-error": "1.0.5",
"apollo-link-http": "1.3.3",
"react-native": "0.52.2",
I did not check it on android.
Edit: I have to dig deeper but I think this might be related to queries being fired before the cache has been hydrated (which also fail and return null because the authToken has not been set on the query yet).
Edit 2: It is this ^
from apollo-cache-persist.
I tried to work around by manually restoring the cache but it results in an infinite login logout loop...
userLoggedCacheRestore = async () => {
const query = gql`
query UserLoggedInfos {
userLogged {
id
}
}
`;
const { client } = this.props;
const result = await AsyncStorage.getItem("apollo-cache-persist");
const cache = JSON.parse(result);
console.log(
"=================cache.ROOT_QUERY.userLogged==================="
);
console.log(cache[cache.ROOT_QUERY.userLogged.id]);
if (cache.ROOT_QUERY.userLogged) {
client.writeQuery({
query,
data: {
userLogged: cache[cache.ROOT_QUERY.userLogged.id]
}
});
}
};
render() {
if (this.props.queryUserLoggedInfos.networkStatus === 1) return null;
const { userLogged } = this.props.queryUserLoggedInfos;
if (!userLogged) {
this.userLoggedCacheRestore();
return <LoginSignupNavigator />;
}
from apollo-cache-persist.
@softhib I do not know if you have the same issue. For me it worked out to first start rendering components that fire queries after the Promise
returned by persistCache
resolves.
from apollo-cache-persist.
@n1ru4l Ok ! How did you to that ? Can you write a snippet ?
from apollo-cache-persist.
Thanks a lot @n1ru4l ! I have a little question : where persistCachePromise
come from ? Is it a package or something you code yourself ? Because I don't see inside your folder "./apollo-client-setup"
from apollo-cache-persist.
Basically the file exports the promise returned by the function persistCache
from apollo-cache-persist.
Ok understood !
from apollo-cache-persist.
Finally the problem still persists... 😟 Probably not the same issue. I have to investigate what is emptying the userLogged props. In my case, the cache is well restored before the queries being fired. The user is logged just few milliseconds and then is logout by something I don't spot...
from apollo-cache-persist.
I need to know what triggers the third render.
from apollo-cache-persist.
The issue is probably related to apollographql/apollo-client#2920
from apollo-cache-persist.
Sorry to hear about the problem, @softhib.
I think you're right about this being a problem in apollo-client
. I can't say for certain, but I think a network error is causing the third render to be null on iOS.
The fact that [apollo-cache-persist]
logs about being restored before all of this happens tells me that it's correctly configured. With that being said, @n1ru4l's advice to await the promise returned by persistCache
is always a good idea — it ensures that the cache has been restored before you start rendering.
I'm going to close this issue for now, but if you find reason to believe that the problem is actually in apollo-cache-persist
, please feel free to reopen.
from apollo-cache-persist.
Related Issues (20)
- Prevent user to close browser tab when there is pending writes HOT 4
- Feature Request: Associated time that given fields/entire cache was last updated HOT 1
- PersistMapper/PersistLink doesn't support nested cache item reference
- [Discussion] Allow for individual cache items to be saved instead of current single-key implementation HOT 1
- Don't persist @client fields? HOT 3
- AsyncStorage or window.Local storage should I use on Web? live LocalStore HOT 2
- Lazy loading cache HOT 1
- "this" has been rewritten to "undefined" HOT 1
- Why was the persistLink and persistenceMapper example removed? HOT 2
- [Request] - Please update code for MMKVStorage instance creation as per their new implementation HOT 1
- Duplication of entire / partial cache object in MMKV storage HOT 7
- [question] What's the recommended way to handle render exceptions caused by stale persisted cached data? HOT 1
- [Question] Collaborative / multiplayer caching / syncing engine? HOT 2
- Calling client.clearStore() does not reset the persistence layer HOT 4
- this.fire is not used in this case
- useLazyQuery not returning the cache value when I request on click or with delay. HOT 1
- Help for adding support apollo client next js HOT 2
- The UI is not updated when mutating the cache with writeFragment
- Is it possible not to persist data to storage if it's undefined ? HOT 4
- Garbage collection isn't working with cache persistance HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-cache-persist.