I am currently doing a project in which I need to implement a logic when the user posts when offline. For ex. when a user goes offline and he presses Save(to a database on cloud), I don't want those data to be lost, I want those data to be saved as soon as the connection is back.
I have not found anything specific even after hours of search.
Thank you for any suggestion!
You can use NetInfo to check connection status, then if the user is offline, you can store their post using AsyncStorage. Once NetInfo has detected that the device is online again, it can pull from AsyncStorage any posts that were saved, upload them, and clear their keys.
Related
Hi I was really hoping to get some guidance with React Native. I'm currently building an App using expo. It's a two part question.
was how to go about storing data when the internet connection is lost. In Africa, internet connection can be lost at any time.
Currently, once the user is logged in, I am saving data into state using Context API. I save my access token on Secure store and some user data on Async storage. However these dont store large amounts of data. How reliable and efficient is SQLite and realm and would you recommend any other option? Realm is not supported on Expo Go however it is seeming to be the best option.
and 2) Supposing I used SQLite or any other option to store data on the phone, how to i go about updating the data on the phone with the newest data on the server once internet connection comes back. I was thinking to overide the entire data?
I made a complete app using firebase, expo and react native (with login screen and other features). But it didn't work as I expected because I need an app that the database works offline (I want the user to use the app offline, and then connect to the internet and "update" the data).
In other words, I'm looking for another way. I heard about Realm, but it doesn't work with Expo (from what I've researched). Can anyone guide me to some possibilities??
About the app I'm making: login>selection>release
*the user logs in (offline. The first time can be online, but when entering the app again, the login must be OFFLINE). The next screen is 'selection' (all screens show information of the logged in user), the user writes some information that is saved in a database. When completing the 'selection', the next screen is 'release', in which the user will add information in the same data table (db) before (from the 'selection' screen). The user will not have internet when entering the app (but he can access the internet later and update his data as soon as he connects).
Firebase work offline and import the data when it is online.
Now about the other db that works with expo, if you like working with SQL then there is something I built expo-sqlite-wrapper it is a good ORM and did not find any better.
There is also a JSON based database easy-db-react-native, the downside of this is that you can't have too much data saved.
been working on this app with react native and have been implementing persistence for some functionality with Async Storage.... but a big detail kinda flew over my head.
For the user to use the app they have to log in, and it works perfectly for one user, all data is kept and all..... but if on the same device, a different user logs in then I get the data stored in the async storage from the previews user... which is kinda of obvious, but didn't even think of that.
I can't seem to think of a way I could differ that info for different users? the only data I could use to differ each user would be the email they use to log in... would I have to create a different entry on the storage with the email as a key and then do a logic to check if it is theirs or some other users info?
any ideas on the best practice for this situation?
Thank you in advance
You could make your keys for different users distinct by starting each key with a user's email. Example:
Joe:
AsyncStorage.setItem('joe#website.com/userId', jsonValue)
James:
AsyncStorage.setItem('james#website.com/userId', jsonValue)
Another option is that every time a user logs out of your app you could clear the AsyncStorage using await AsyncStorage.clear().
I am building an sample application that lets user store comments.
I've created the registration and login process. When the user registers, his details are stored in a MySQL database and a token is returned to the browser. Now he can access the Profile page.
When an existing user logs in he is redirected to the profile page. The profile page is accessible only when a user registers or logs in.
After logging in, I want to show all his comments if he has already added them.
My frontend is in Angular and backend use Laravel. For authentication I use Satellizer.
I want to know, what is the best approach while playing with data, considering the fact that the user will add, edit his comments. Should I use localstorage and store data in a key value pair or should I create a json file which gets updated everytime the user adds a comment or makes a change.
I wanted to know what is the most efficient way to deal with data from server so that the application is fast even when it scales to a 10000 users and lot of data for each user.
Thanks
You should be updating it on the server when changes are made rather than only relying on localstorage. You can use localstorage to cache, but it should only be for immutable data, it shouldn't really be used for data that is going to change.
So in this case you'll be adding and updating new comments via your API (ideally a RESTful one!). Once you've made a change, you could store the comments locally and only update them when the user makes a new comment, however you'll quickly run into issues where the data is invalid on different clients. (i.e. if you update the comments on a different computer, the other computer won't be aware).
Alternatively, you could cache the comments and then simply ping the server to find out if new comments have been added. This could be using a HEAD request for example to check the last modified date on your comments resource.
You can store comments data locally on user browser, but you should properly manage it.
I don't how much load your server will have and if the time invested now worths it.
You can fetch comments and store them locally
User adds a comment, then you update locally and send a request to the server
You need to track the request response, if requests fail so notify user and remove comments from local.
if request was successful so you can continue on your way.
** facebook uses this "success first" approach
user does an action, and he see it happens instantly, in the background it could take few seconds, only if it fails they will notify you.
** look at their commenting process, when you comment, it appears instantly, no loading... but in the BG the load happens.
I'm building a site where I want to allow users to keep wishlists of movies they want to see and movies they already have seen. To do this I want to use data from the movie tmdb, but I'm not sure how to handle this.
What if a user comes on my site and enters the query 'Batman', what is the next step I should take?
Search my own database for 'Batman'
Search API for 'Batman'
Merge results from own database and external and print, but don't save anything to my db
If a user then clicks on a result that's not in my database I would do another request to the API for the more detailed information, also saving images and so on before showing it to the user.
Is this the way I should go about this or is there a better way?
You should browse API for this movie. Data in TMDB Api is often changing, so I suggest you not to store it for a long time in your db.