Persisting data in meteor for mobile (android and iOS)

I know this question has been asked a lot but there doesn’t seem to be a consensus on a working solution.

I would like my meteor app to be able to save some data and have access to it offline on mobile (android and iOS). I have taken a look at persistent session but its no longer maintained.

Is there a workable solution yet?

I believe every phone has a memory, I was wondering if there’s a cordova package that could be added to take advantage of a phone’s internal storage or something of that nature.

1 Like

Cordova plugin to the rescue: https://github.com/TheCocoaProject/cordova-plugin-nativestorage

Thanks @rjdavid.

I’ve taken a look at the project and I’ve taken steps to use it.

I installed with meteor add cordova:cordova-plugin-nativestorage@2.3.2

I have been at this for hours without success. I followed everything in the docs.

Here’s what I’m doing inside a react component.

        if (Meteor.isCordova) {
            NativeStorage.setItem(
                "myCountry", {COUNTRY: 'NIGERIA'}, 
                () => alert(`Your country is set to NIGERIA`),
                () => alert("Your country is not set"));
            this.setState({set some state})
        }

To read the value I’m using

NativeStorage.getItem("myCountry"). This returns undefined everytime. I don’t even know if the item is set in the first place.

I looked at the example here https://github.com/TheCocoaProject/cordova-plugin-nativestorage#example, there is nowhere getItem is ever used to log the value that is supposedly saved.
I have tried every trick. Scoured posts. Uninstalled and reinstall all to no avail. I don’t know if I need to do any kind of imports in my file.

But in my phones dev console I have access to NativeStorage so I suppose its available without imports. But even in my dev console getItem always return undefined

It’s really frustrating. How do I go about this?

Finally solved my puzzle. I looked in the example demo here and found the exact way to use it

NativeStorage.setItem("dummy_ref_obj",
    data,
    function (result) {
        alert("Saved Data : " + result);
    },
    function (e) {
        fail("Write Object Failed");
});

NativeStorage.getItem("dummy_ref_obj",
    function (result) {
        alert("Current Stored Value was: " + result);
    },
    function (e) {
        fail("Read Object Failed");
});
2 Likes

Excellent tutorial and very well explained! krogerfeedback