AsyncStorage.setItem 每次都让 iOS 崩溃,在 Android、Expo 上完美运行

use*_*160 3 ios react-native expo

这是我要导入的内容:

import React, { Component } from 'react'
import {
  Image,
  Text,
  TextInput,
  Keyboard,
  KeyboardAvoidingView,
  TouchableOpacity,
  View,
  Linking,
  AsyncStorage,
  Alert
} from 'react-native'
Run Code Online (Sandbox Code Playgroud)

这是设置状态的函数(从下面的 TextInput 调用)

setName = (name, value) => {
    AsyncStorage.setItem(name, value);
}
Run Code Online (Sandbox Code Playgroud)

这是它的调用方式

<TextInput
    keyboardType='default'
    autoCapitalize='words'
    returnKeyType='next'
    onSubmitEditing={() => this.lastNamedRef.focus()}
    underlineColorAndroid='transparent'
    style={{flex:1}}
    placeholder='Your first name'
    value={this.state.firstName}
    onChangeText={(text) => [this.validateInputLength(text, 2, 50, 'firstName'), this.setName('firstName', this.state.firstName)]}
 />

<TextInput
    keyboardType='default'
    autoCapitalize='words'
    returnKeyType='done'
    ref={lastNameRef => this.lastNamedRef = lastNameRef}
    underlineColorAndroid='transparent'
    style={{flex:1}}
    placeholder='Your last name'
    value={this.state.lastName}
    onChangeText={(text) => [this.validateInputLength(text, 2, 50, 'lastName'), this.setName('lastName', this.state.lastName)]}
 />
Run Code Online (Sandbox Code Playgroud)

如果我从 TextInput 的 onChangeText 中删除 this.setName() 函数,应用程序不会崩溃,但是当它在那里时,它会在 iOS 上崩溃,但不会在 Android 上崩溃。this.state.firstName 和 this.state.lastName 来自 AsyncStorage 当 componentWillMount()(下面)使用 setName 函数(上面)更新时

componentWillMount() {
    AsyncStorage.getItem('firstName').then((value) => this.setState({firstName:value}))
    AsyncStorage.getItem('lastName').then((value) => this.setState({lastName:value}))
}
Run Code Online (Sandbox Code Playgroud)

componentWillMount() 中的 AsyncStorage.getItem 不会使应用程序崩溃,但似乎不会不一致地将数据加载到字段中。大多数时候它会加载,但不是每次都加载。AsyncStorage 一般看起来超级有问题。

这是 validateInputLength 函数

validateInputLength = (text, min=10, max=25, type) => {
  if(text.length < min || text.length > max) {
    if(type=='firstName') {
      this.setState({firstName:text})
      this.setState({firstNameValidated:false})
      return false;
    } else if (type=='lastName') {
      this.setState({lastName:text})
      this.setState({lastNameValidated:false})
      return false;
    }
  } else {
    if(type=='firstName') {
      this.setState({firstName:text})
      this.setState({firstNameValidated:true})
    } else if (type=='lastName') {
      this.setState({lastName:text})
      this.setState({lastNameValidated:true})
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

...这是构造函数

constructor(props) {
  super(props)
  this.state = {
    firstName: '',
    lastName: '',
    firstNameValidated: false,
    lastNameValidated: false,
  };
}
Run Code Online (Sandbox Code Playgroud)

无论是在我的 iPhone 上还是在模拟器中运行,这都会使 iOS 上的 expo 崩溃,而不会显示任何错误。有任何想法吗?

小智 10

如果您尝试存储 JSON 对象(例如 expo 崩溃),则只能将字符串存储在 AsyncStorage 中,这可能是正在发生的事情,请在存储对象之前尝试使用 JSON.stringify

  • 一旦我存储了 JSON.stringify(myObject) 而不是 myObject,这就奏效了!谢谢! (2认同)