Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local database + Server database - React Native

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.

  1. I thought using Realm for local storage
  2. MongoDB for remote storage

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?

like image 819
b-user Avatar asked Feb 17 '26 11:02

b-user


2 Answers

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.

like image 186
stever Avatar answered Feb 20 '26 06:02

stever


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.

like image 40
Jeff Gu Kang Avatar answered Feb 20 '26 06:02

Jeff Gu Kang