Skip to content
Advertisement

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>

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement