Backup data with Async Storage - React Native - react-native

I am creating a note-taking app.
I want to enable users to backup their data manually by clicking 'backup' button, which will create a db backup file.
Upon reinstalling the app, users should be able to restore their data by importing the db backup file.
Would this be possible? I've already seen posts about using Gdrive and ICloud, but this is automatic. I wish to export and import the db file of AsyncStorage manually. For example, expecting API something like AsyncStorage.createDbFile or equivalent.
Alternative would be to JSON.stringify all the data in AsyncStorage and put that on a txt file and export that, but I don't think this is a good idea (or is it perfectly fine to do this?).
Thank you!

Related

How to backup and restore an SQLite database?

I have an app built using React-Native and Expo's Managed workflow. I'm using Expo SDK 45. I'm managing the db using 'expo-sqlite'.
I need to provide the user the capability of backing up and restoring a local SQLite database.
My attempt was to use 'expo-file-system' and 'expo-sharing' to allow the user to save a copy of the database to the location of their choosing. This was done successfully.
My issue is in the user restoring the database. What I'm trying:
const importedFile = await DocumentPicker.getDocumentAsync();
await FileSystem.copyAsync({ "from": importedFile.uri, "to": FileSystem.documentDirectory + 'SQLite/tempDB.db' });
I then verify tempDB.db to ensure the structure is correct, i.e. the user isn't trying to import a wrong file.
I now have 2 issues:
If the user tries importing another database, I can delete the current tempDB.db but when the new one is imported, any attempt to access the new database fails with error Error code 10: disk I/O error. It seems that perhaps the system still believes it has an open connection to the deleted database.
If I overwrite the actual app database with the imported one, again there is no connection and nothing loads.
If I manually restart the app, all database connections are opened with the new files and everything works.
So, is there a better way to attempt this backup/restore functionality or is there a way to force the user to restart the app?

Storing large data in react native for offline usage

For our app we need the app the work offline 100% with the most recent data.
Normally the app uses a symfony api platform API to fetch data paginated server side.
But when someone is using the app in a area without Internet he still need to be able to access all data to make a new job and que it for when he is online.
So our tough is to make a json that has all the data and send it to the phone when the phone has connection.
When the phone is using the app live, it will use pagination from server and fetch data.
And when offline it will use the local downloaded data file.
Problem is the data is large, so storing it is the problem.
We are now using redux persist and we see so many people have problems with crashes and mobile ram.
We also thought of using sqlite or just async to store a json file, or download a sqlite file from the app.
How do you guys feel about this
You can use react-native-local-mongodb. This can help you store a json database on your phone.
Other option is to have react-native-sqlite-storage in case your database has to be relational and requires table.
You will have to sync this local copy with the server once this user is online. This too can be done if you have the timestamp saved of the last sync. So after that, you ll just have to sync the data created after that timestamp.
However, I had also implemented react redux persist and it didnt crash. I suppose, the reason for crash must me something else.

Best way to save json data and files in react native?

I am developing a react-native application. I want it to get a json file online and print it as a list, showing the image and by clicking, showing the pdf. Here is my json :
[
{
"id":"1",
"nb_edit":0,
"nom":"Catalog 1",
"description":"test",
"apercu":"img\/1.png",
"pdf":"pdf\/1.pdf"
},
{
"id":"2",
"nb_edit":"0",
"nom":"Hi",
"description":"yes",
"apercu":"img\/2.png",
"pdf":"pdf\/2.pdf"
}
]
It actually works fine. But now, I want to add an offline mode. For this, I want to basically set a version variable in the application to 0, and check if the version on the website (version.txt) is the same. If yes, just load the json file saved in the phone (the png and pdf are saved too). If not, download the json, update the json saved locally, update the version variable, and then load the files from the phone.
Do you have an idea of how could I do? I thought about using redux-persist for the version, but will it work for the json, the images and the pdfs, and how?
Thanks for your help.
You can use :
Option1:
AsyncStorage is an unencrypted, asynchronous, persistent, key-value
storage system that is global to the app. It should be used instead of
LocalStorage.
https://reactnative.dev/docs/asyncstorage.html
Option2:
Local database
SQLite is an open-source SQL database that stores data to a text file
on a device. It supports all the relational database features. In
order to access this database, we don’t need to establish any kind of
connections for it like JDBC, ODBC.
react-native-sqlite-storage
https://www.npmjs.com/package/react-native-sqlite-storage
I would not try to save the file to the device. Instead of trying to save the file, I would save data to the device's storage by using AsyncStorage (If the file is not that big). If you go in this way, do not forget to stringify the object while saving the object to the storage.
You may use react-native-fs for saving files such as pdf, images.

Export Application Settings to Shared Network Directory - Load / Import Settings on Startup

Can I export my application settings to a shared network drive and then 'point' users to it, essentially using this as a project settings 'ini' file.
I am referring to the system generated settings file created by the application. i.e. C:\APP_NAME\My Project\Settings.settings
For example I wish for all users on a particular project to use the same database connection string which is persisted in the application settings.
I would like the following functionality:
Administrator exports their application settings.
Users point to the exported settings file.
Settings file is used at startup for all users.
I have written a class to export a .txt file and transpose the settings at startup, but surely importing and exporting the settings file itself is a much more efficient and less error prone solution.
Thanks...
I think what you have done is correct. If you have written a method to read / import the settings at start, is this what you plan to achieve?
Couple of questions to go further into this...
What kind of settings are you actually trying to load? Are these startup dependent? or can they be loaded into the program at a later time? I.e. after the initial start / form has loaded?
Have you got access to SQL or MYSQL or similar? - If so you could write a small table with list of users or user groups and settings, then retrieve these using a hardcoded connection string (or another way you please), this can then be updated / modified by other 'Specified' users
It might even be nice to create a users table / csv to hold each user and then their permissions / group.
If you are not using SQL / MYSQL how are you making sure the text file cant be read, altered? Maybe look into some simple encryption (Its VERY easy, if this is needed at all?)

Ideal location of db on IOS

I am using phonegap for IOS app development. Now, phonegap tries to find the db by default in 'NSCachesDirectory', but whenever IOS runs into memory problems, it tries to delete data from 'NSCachesDirectory', so, the data is not secured. If i am not mistaken, this problem was solved in cordova 2.1.0, where the back-up of data is taken and then restored afterwards. So, just wanted to confirm if i am heading in the right direction or data itself can be stored in 'NSDocumentDirectory' so that data is secured and somehow phonegap looks for db in 'NSDocumentDirectory' and not Caches direcory. Thanks.
Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the /Documents directory(You can store DB in Documents directory)
Data that can be downloaded again or regenerated should be stored in the /Library/Caches directory.