React Native Mobx空指针取消引用 [英] React Native Mobx null pointer dereference
问题描述
我正在使用mobx(和mobx-persist)作为我的本机应用程序全局存储(我正在使用expo客户端).首先,我从服务器获取一些数据,然后将结果保存在存储中,并将其持久保存在asyncStorage中.最初它运行良好,但随后突然出现问题,当我尝试登录该应用程序时冻结,然后崩溃,并包含原因:空指针取消引用"的摘要.以下是我的代码.
I'm using mobx (and mobx-persist) as my react native app global store (I'm using expo client). First I fetch some data from the server then i save the results in the store and persist them in asyncStorage. It was working fine at first but then suddenly when I try to login the app freezes then crashes with summary containing "Cause: null pointer dereference". Following is my code.
商店代码:
import { observable, computed, action, set } from 'mobx';
import { create, persist } from 'mobx-persist';
import { AsyncStorage } from 'react-native';
class Store {
URL = 'XX.XX.XX.XX'
@persist('object') @observable db = {
authToken: '',
buses: null,
term: null,
}
@computed get buses () {
return this.db.buses
}
@computed get term () {
return this.db.term
}
@computed get authToken () {
return this.db.authToken
}
@action setAuthToken = (token) => {
this.db.authToken = token;
}
@action setBuses = (buses) => {
this.db.buses = buses;
}
@action setTerm = (term) => {
this.db.term = term;
}
}
const hydrate = create({
storage: AsyncStorage,
jsonify: true
})
store = new Store()
storeLoader = async () => {
await hydrate('object', store)
return store
}
hydrate('object', store)
module.exports = { store, storeLoader }
登录屏幕代码:
import { store } from './store'
import { observer } from 'mobx-react';
fetch(`http://${store.URL}/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: this.state.email,
password: this.state.password
})
}).then(res => res.json()).then(res => {
if (res.token) {
store.setAuthToken(res.token)
store.setTerm(res.term)
store.setBuses(res.buses.sort((a, b) => {
let first = a.fields[0].time, second = b.fields[0].time;
let firstArr = first.split(":", 2), secondArr = second.split(":", 2);
if (firstArr[0] !== secondArr[0])
return firstArr[0] - secondArr[0];
else
return firstArr[1] - secondArr[1];
}))
this.props.navigation.navigate("App")
}
if (res.error) {
this.setState({error: res.error})
}
}).catch(err => console.log(err))
AuthToken和术语均已保存,但总线在登录时或返回应用程序时返回null. 经过调试和搜索数小时后,我发现有些人面临类似的问题,因为他们试图保存嵌套对象的大数组,所以我认为也许buss数组实际上是如此之大(不是这样),所以我尝试仅用1个键保存对象公交车中使用-值"对,但没有成功.
Both the AuthToken and the term are being saved but the buses is either returning null when logged or crashing the app. After debugging and searching for hours I found some people facing similar problems because they were trying to save big arrays of nested objects so I thought maybe the buses array is actually that big (tho it's not) so I tried saving an object with only 1 key-value pair in buses but with no success.
推荐答案
使用自定义类的持久列表修复:
Fixed by using persist list of custom class:
在类商店中:
@persist('list', Bus) @observable Buses = []
和上公交车:
class Bus {
@persist @observable someField1 = ''
@persist @observable someField2 = false
@persist('list', field) @observable fields = []
}
偶数类总线使用了另一个类字段:
even class Bus used another class field:
class field {
@persist @observable someField1 = ''
@persist @observable someField2 = ''
}
这篇关于React Native Mobx空指针取消引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!