Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
AsyncStorage setItem inside loop crashes

App crashes when trying to save something into asyncStorage. It crashes without an error message however I think the problem is in the while loop:

const saving = () => {
console.log("ran") // gets called
let loop = true
               let cont = 0
               while (loop == true) {
                    console.log("got here") // Doesn't even get called
                    AsyncStorage.getItem(`Item-${cont}`, (error, result) => {
                         console.log("here")
                         if (error) {
                              Toast.show({
                                   type: 'error',
                                   text1: 'An error has ocurred!'
                              })
                              loop = false
                         } else if (result == null) {
                              AsyncStorage.setItem(`Item-${cont}`, (error) => {
                                   console.log("there")
                                   if (error) {
                                        Toast.show({
                                             type: 'error',
                                             text1: 'Error occurred while saving workout!'
                                        })
                                        loop = false
                                   } else {
                                        Toast.show({
                                             type: 'success',
                                             text1: 'Workout saved successfully!'
                                        })
                                        loop = false
                                   }
                              })
                         }
                    })
                    cont++
}

As soon as the loop gets executed, the app crashes, console.log("got here") doesn't even run. This function is called from a Pressable component:

<Pressable
   onPress={() => saving()}
>

</Pressable>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

So i don't really understand why you saving individual items, you could serialize an array and save it all at once, like that:

await AsyncStorage.setItem('data', JSON.stringify(data));

Also when you tried to call AsyncStorage.setItem, you have an key and a call back, but i don't see any values being set, so i think you have to do something like this:

await AsyncStorage.setItem(`Item-${cont}`, value, (error) => { /* callback */ });

I don't think its a good idea get items then immediately saves, you should get all the data at the start then save it at some point, and consider that localStorage it's not a database.

You also can check the documentation about AsyncStorage for reactNative here

about 4 years ago · Juan Pablo Isaza Report

0

I had to change it from a while loop to a for loop, what I did was the following:

for (let i = 0; i < cont; i++) {
                    await AsyncStorage.getItem(`Workouts-${i}`, (error, result) => {
                         if (error) {
                              Toast.show({
                                   type: 'error',
                                   text1: 'ERROR',
                                   text2: 'An error has ocurred!'
                              })
                              cont = 0
                         } else if (result == null) {
                              var object = {
                                   name: rec_workoutName,
                                   exercises: rec_renderedArray,
                                   difficulty: rec_workoutDifficulty,
                              }
                              AsyncStorage.setItem(`Workouts-${i}`, JSON.stringify(object), (error) => {
                                   console.log("saved")
                                   if (error) {
                                        Toast.show({
                                             visibilityTime: 2000,
                                             type: 'error',
                                             text1: 'ERROR',
                                             text2: 'An error has ocurred!'
                                        })
                                        cont = 0
                                   } else {
                                        Toast.show({
                                             visibilityTime: 2000,
                                             type: 'success',
                                             text1: 'SUCCESS',
                                             text2: 'Workout saved successfully!'
                                        })
                                        cont = 0
                                   }
                              })
                         }
                    })


                    cont++
               }

I hope this helps someone who encounters the same issue.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!