Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda