Search code examples
react-nativeasyncstorage

AsyncStorage mergeItem - Unrecognised selector


Hi I'm trying to do a simple get and update using AsyncStorage in react native.

I get the data in componentDidMount() using the following code:

   AsyncStorage.getItem('mytrips', (err, result) => {
      if(result !== null){
        var data = JSON.parse(result);

        console.log(result);
        this.setState({
          email: data.email,
          firstname: data.firstname,
          lastname: data.lastname,
          phone: data.phone,
          mobile: data.mobile,
          phonecode: data.phonecode,
          DOB: moment(data.DOB).format('DD MMM YYYY'),
        });
      }
    });

Following some user input I want to update what's in storage using mergeItem.

AsyncStorage.mergeItem('mytrips', JSON.stringify(data));

data is an object of all the changed values.

I get the following error when mergeItem is called.

ExceptionsManager.js:71 Exception '-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 0x600000886400' was thrown while invoking multiMerge on target AsyncLocalStorage with params (
        (
                (
            mytrips,
            "[\"Carl\",\"Carl\",\"11111111\",\"111111111\",\"\",\"1900-01-01\",\"carl@test.com\"]"
        )
    ),
    3657
)

Any idea what could be causing the selector to be unrecognised? I'm not calling removeItem anywhere in my code.


Solution

  • You are merging an array of items as I can see. You can't merge an already stored JSON item with the array of items. As stated in the react-native docs:

    MergeItem: Merges an existing key value with an input value, assuming both values are stringified JSON. Returns a Promise object.

    Please try and merge the JSON object with the already saved item to work properly. I hope this solves your problem and if not please let me know :)