ios - AsyncStorage.setItem 每次都会使 iOS 崩溃,在 Android、Expo 上完美运行

标签 ios react-native expo

这是我要导入的内容:

import React, { Component } from 'react'
import {
  Image,
  Text,
  TextInput,
  Keyboard,
  KeyboardAvoidingView,
  TouchableOpacity,
  View,
  Linking,
  AsyncStorage,
  Alert
} from 'react-native'

这是设置状态的函数(从下面的 TextInput 调用)
setName = (name, value) => {
    AsyncStorage.setItem(name, value);
}

这是它的调用方式
<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)]}
 />

如果我从 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}))
}

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

这是 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})
    }
  }
}

...这是构造函数
constructor(props) {
  super(props)
  this.state = {
    firstName: '',
    lastName: '',
    firstNameValidated: false,
    lastNameValidated: false,
  };
}

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

最佳答案

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

关于ios - AsyncStorage.setItem 每次都会使 iOS 崩溃,在 Android、Expo 上完美运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48234708/

相关文章:

ios - NumberFormatter 字符串(来自 :NSNumber) method has issues with decimal places

iPhone 照片裁剪

ios - iOS SDK 中基于服务器的通信

node.js - 在 bundleReleaseJsAndAssets 处生成发布 APK 失败

react-native - 如何解决使用 AsyncStorage (deprecated) 警告?使用社区(正确)库

react-native - Expo Build,自动更新我在 App Store 和 PlayStore 上的实时应用

javascript - 我们是否需要使用 Expo 项目运行 pod install

ios - 无法读取资源文件夹中的文件-iOS

javascript - react native : synchronously run functions

android - 如何使用 react-native 在 textInput 字段中自动获取 '/'(斜线)