I ask myself lots of questions about the choice of databases for my future react-native application, because I would like a local database that stores the static data of the application. I would like also to store the data created by the user on a data server.
Problem: I do not know how to synchronize remote data, because when the user is offline, I want the data to be stored locally and then when it goes offline, I want the data to be sent to the server
Can these two databases be used mutually, or is MongoDB able to store the data locally?
Consider using AsyncStoragefor local storage, andAppState to sync it with your db of choice (mongoDB is a good remote choice, but doesn't work locally). They're both native modules. AsyncStorage works like localStorage. When the app transitions from foreground to background is a good time to save data locally and remotely. It looks like this.
_handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
console.log('App has come to the foreground!')
this.setState({appState: AppState.currentState});
}
else if (this.state.appState.match(/active|background/) && nextAppState === 'inactive') {
console.log('App has gone to background!')
this.setState({appState: AppState.currentState});
}
}
The documentation is excellent.
I am not sure how many data will you treat in local database, but MongoDB Stitch can be the one of solutions.
Stitch Mobile Sync.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With