Running cleanup when a react native app is closed - react-native

In my react-native app's code:
I am monitoring AppState changes
When AppState changes to 'inactive' or 'background', I write about 2000 key/value pairs of data to AsyncStorage
When the app starts I read this data
When I test the app on android (didn't test on iOS yet):
If I minimize the app, the data is written as expected. I can see that by closing the app after minimizing it, and restarting it
However, if I click on the 'Recent' button in the navigation bar (which changes AppState to 'inactive') and immediately click on the 'X' button at the top right of the app, the data doesn't seem to be written, or at least not all of it (I am not checking all 2000 values, just few of them). If I try to write only few values, they are written
Question:
The explanation to the above behavior seems to be simple: the app has enough time to perform few operations before I click on 'close', but not to write 2000 values. Is there a way to perform the writing before the app is closed?

Actually it's not recommended to do any async function or anything that could take too long. Because you don't have the control of how much time it will take to write the 2000 values. The app can go to background and the SO can kill the proccess of your app. When the app is in background is the SO that controls it.

Related

React Native - Execute code in background on specific battery percentage

I have a react native application where the user can choose any battery level from 15 to 100. When his battery reaches the chosen value, javascript code should be executed.
Is there a way to do this when the app is in the background? I am aware I can have long running tasks, which will be displayed as a notification to the user and I can have my code logic there.
Are there any alternatives that do not include long running tasks?
My solution for this problem to register a BroadcastReceiver in android, which will be executed every time when the battery percentage changes. It works in background and if the battery reaches a certain threshhold, a headless task can be executed which will run javascript code.

React Native - console.log and network commands are not working

I've a strange problem in app I'm currently coding.
Here is the story of the app :
I've used React Native's ScrollView as a horizontal slider,
I display maximum 5~6 slides so I don't need to use FlatList for this.
Slides are actually records coming from the database so actually they are some dynamic components
and slider works as expected.
In every slide, there are also are some option buttons (Touchables) to send data to the server.
When the user presses a button app is opening a modal window to confirm and then sending some data to server.
Until now all is okay.
The Problem :
But in some slides of the slider I'm having a strange problem :
"console.log" commands and also network commands to send data to server is not working.
On the screen, I see Buttons(Touchables) are working and also the modal I've coded is also appearing & disappearing according to the state variables. But somehow console.log commands and also network commands are not even executed. Since I can't log anything it's also hard to understand the problem.
Is there anyone had a similar problem ?
Thanks
I've finally solved this problem, wanted to share my experience here.
In the app I was making post requests to the backend with Axios,
but I wasn't interested in the result of this post requests,
they were just log records so the result of backend calls weren't important.
So I didn't use any "await" command or didn't code anything like Promise.then / catch,
just posted with Axios and scrolled to another slide in my app without waiting for the backend.
After the 8th Axios post, app started not to work.
It seems like working .. touchable effects , modals even navigation works
but nothing else was working. Event console.log commands weren't working.
It's an interesting behavior of React Native , but I've understood the situation after reading the blog below :
https://medium.com/#rotemmiz/react-native-internals-a-wider-picture-part-1-messagequeue-js-thread-7894a7cba868#
Basic reason was that in the backend (NodeJS) I didn't return any response,
there wasn't any command like res.send("success") .. so frontend React Native app was waiting for the response.. event if I didn't use any await or Promise it was still trying to get the response and after the 7-8 call it was blocking the main thread.
If there is a way to configure Axios not to wait for the backend to answer for the post request, please write here.

React-native redux navigation state reset after crash

We are using redux navigation and persisting redux state. However, sometimes when the user navigates to the page and an unexpected error occurs app crashes with a white screen. If the app is launched again it obviously crashes again because the same state is loaded. As the result app becomes unusable - the white screen crash straight after the app launches.
I guess it is fine to crash the app if there is a bug on one of the screens, but because of one buggy screen I don't want to make the entire app unusable for a user and it would be great to somehow reset a state and enable the user to keep using other functionality of the app.
Any ideas on how to achieve that?
I think the real question is: do you want to persist navigation state at all? Do you really want to load last visited screen on app launch rather than the first screen of the app? I've never seen an app that does this
Another question is how do you persist your navigation state in redux. It was only a pattern when using older versions of react-navigation (v1 and v2 if I recall correctly) but even then it wasn't the optimal pattern to keep navigation state in redux (redux integration of react-navigation v1), let alone persist it on restarts. Navigation is something that should start fresh every time user launches the app
Also read about state persistence in current version (v5 - https://reactnavigation.org/docs/state-persistence) usually there is no redux involved at all
About state persistence: usually you only persist long-living things like auth state, authorization tokens, user settings, but not some dynamic data that gets discarded often. For example, if you open some page and fetch data to display on that page, there is no reason to persist that data in AsyncStorage, because why would you? This data should be reloaded every time page opens instead of restored from persisted state. Redux-persist lets you whitelist or blacklist different parts of the state
To summarize:
1. figure out if you need to persist navigation state at all. If not, problem solved
2. if you do, try to setup redux-persist the way that it doesn't persist short-living error-prone data
3. figure out how navigation state is persisted (through redux or on it's own, see examples in the links I provided earlier)
4. integrate react-native-exception-handler, catch exceptions and reset persisted navigation state in case of a crash

How can i change the background of my App after it approved and already in App Store

I am new in Xcode and IOS development.
i have designed and finished my app and i connected with Parse for push notification and core data.
the problem is what i didn't understand is: if in future i want to change the background of my app or add new event or to change the palace of button my app, how can i do that? i have to rebuild and submit it again or there is any way to do by online a website like parse?
i couldn't find the answer any where, help please...
Thanks ,
If you had designed your app to load the background data from Parse, then you could just put the new background on Parse and your app would load it. There is no need to submit a new app to do that. If you did it this way, you'd want to have a default background in the case when the network is not available.
You can't add new code to your application with this method (that is not allowed), but you can add data such as images, text, etc. The key point here is that you have to design your app from the start to work this way, then it is simply a matter of putting the new data on Parse where your app can find it.
Expanding on #vacawama's very good answer:
For this version you are out of luck, since it sounds like you did not design it to use a background that is loaded from your Parse server.
What you need to do is to code an update to your app that has these new abilities, and submit that to the app store. Once that version is approved then you should be able to change the background from the server.

Long-running task performed in foreground is suspended when app enters background

When a user first opens my app, I need to download and install some content from a server before they can begin using the app. The problem is that this takes around 5 minutes on wifi, during which time the app goes into the background and the download is suspended.
Is there any way to either:
prevent an iOS app from entering the background whilst I perform my download
or continue peforming the task in the background (i.e. perform the task irrespective of whether the app is in the foreground or background)
Thanks
It really doesn't matter, if the user presses the home button it will go to background. Although you can do two things to mitigate the problem:
Use beginBackgroundTaskWithExpirationHandler, to give you a bit more time to download. Which you can read here.
Don't allow the device to become iddle, with [UIApplication sharedApplication].idleTimerDisabled = YES;. You can read more about that here.
Either way, the best thing you can do is to tell the user, that is an important download and he shouldn't quit the application.
Can't you include some or all of the content in your app bundle instead, and just download changes on first run?
I can't imagine this is a good first user experience, and it may not pass App Store review like this.
The only third party apps that are allowed to download in the background are newsstand apps loading issue content, and Apple are pretty strict about what they allow as newsstand apps.
You can't do what you want, in this situation. One way, and I think the best and only, is to resume your download when you app becomes active (returns to foreground state). Also, don't forget to register for connectivity notifications (Reachability class can be used for this purpose from this Apple sample app http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html). Good Luck!