Meteor-React: GroundDB suddenly empty

214 views Asked by At

[EDIT] updated to Ground DB v2, made code more readable

I am trying to use GroundDB in my project, so my Meteor-React Cordova App can also run offline. In the main container I have the following code:

let fullyLoaded = new ReactiveVar(false);
let subscribed = false;
let subscribedOnce = false;
let checking = false;

export default createContainer(() => {

    if(Meteor.status().connected && !subscribedOnce && !subscribed){
        subscribed = true;
        console.log("subscribing");
        Meteor.subscribe("localization",
            ()=> {
                localizationGrounded.keep(Localization.findNonGrounded());
                console.log("everything has been loaded once.");
                console.log("Localization count after subscription: " + Localization.find().fetch().length);

                fullyLoaded.set(true);
                subscribed = false;
                subscribedOnce = true;
            }

        );
    }
    if(fullyLoaded.get()){
        console.log("Localization Count: " + Localization.find().fetch().length)
    }
    return {
        isLoggedIn: !!Meteor.userId(),
        isLoading: !fullyLoaded.get() || subscribed,

    };
}, Main);

This code is supposed to subscribe to "localization" if it hasn't been loaded already. The Localization Collection is implemented as follows, the find() and findOne() method has been overwritten to call find() for the grounded DB:

export const Localization = new Mongo.Collection('localization');

if(Meteor.isClient){
    export let localizationGrounded = new Ground.Collection('localization', {
        cleanupLocalData: false
    });


    //rename find() to findNonGrounded
    Localization.findNonGrounded = Localization.find;

    localizationGrounded.observeSource(Localization.findNonGrounded());

    Localization.find = function(...args){
        console.log("finding from ground db");
        return localizationGrounded.find(...args);
    };

    Localization.findOne = function(...args){
        console.log("finding one from ground db");
        return localizationGrounded.findOne(...args);
    }
}

This however produces the following output:

subscribing
everything has been loaded once
finding from ground db
Localization count after subscription: 28
finding from ground db
Localization count: 28

Looks fine, right? Unfortunately, the createContainer() function is called once more immediately after that, resulting in

...
Localization count: 28
//Lots of "finding one from ground db" indicating the page is being localized correctly
finding from ground db
Localization Count: 0
//more "finding one from ground db", this time returning undefined

Please help me to fix this. Thanks in advance

Taxel

1

There are 1 answers

0
paranoico On BEST ANSWER

As far as we have investigated (and found your question meanwhile), this is not a GroundDB error.

Currently we are working in something similar: Also a Cordova App, trying to keep offline about 30 diferent collections and using React too (but with Mantrajs). So as today, we are almost sure that it is the way Meteor functions what is erasing data from Collections, I am going to try to explain myself:

It seems that Meteor in some way detect that Collections are not used and simply remove all data almost immediately, then GroundDB remove the data also from IndexedDB. We downloaded GroundDB code, added it to our Meteor project and inspected it, founding the previous behavior.

Currently, we are trying to find some way to detect when the collection is erased completely, some kind of property on Meteor Collection that could tell us that it is going to be cleared so we can fix GroundDB code and DO NOT remove IndexedDB data in that case.

The other option we are trying is to use this component: https://github.com/richsilv/meteor-dumb-collections

It seems very similar to GroundDB but it never syncs local data to Meteor Server until you call the sync function, so it could be harder to use for us and our 30 collections :)

Hope it helps you.