Search code examples
react-nativenullpointerexceptionmobxmobx-reactmobx-persist

React Native Mobx null pointer dereference


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.

The store 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 }

Login screen Code:

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))

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.


Solution

  • Fixed by using persist list of custom class:

    In class Store:

    @persist('list', Bus) @observable Buses = []
    

    and in class Bus:

    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 = ''
    }