Meteor-React:GroundDB突然空了

时间:2016-11-08 17:30:14

标签: cordova meteor-react grounddb

[编辑] 更新到Ground DB v2,使代码更具可读性

我正在尝试在我的项目中使用GroundDB,因此我的Meteor-React Cordova应用程序也可以脱机运行。在主容器中,我有以下代码:

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

此代码应订阅"本地化"如果它还没有加载。本地化集合实现如下,find()和findOne()方法已被覆盖,以调用基础数据库的find():

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

然而,这会产生以下输出:

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

看起来很好,对吧?不幸的是,之后会立即再次调用createContainer()函数,从而产生

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

请帮我解决这个问题。 提前致谢

Taxel

1 个答案:

答案 0 :(得分:0)

据我们调查(并同时找到您的问题),这不是GroundDB错误。

目前我们正在开展类似的工作:也是一个Cordova应用程序,试图保持离线约30个不同的集合并使用React(但与Mantrajs)。因此,就像今天一样,我们几乎可以肯定,Meteor的功能就是从集合中删除数据,我将尝试解释自己:

似乎Meteor以某种方式检测到未使用集合并且几乎立即删除所有数据,然后GroundDB也从IndexedDB中删除数据。我们下载了GroundDB代码,将其添加到我们的Meteor项目并检查它,创建了以前的行为。

目前,我们正在尝试找到一些方法来检测集合何时被完全删除,Meteor Collection上的某种属性可以告诉我们它将被清除,以便我们可以修复GroundDB代码并且不要删除IndexedDB那种情况下的数据。

我们尝试的另一个选项是使用此组件:https://github.com/richsilv/meteor-dumb-collections

它似乎与GroundDB非常相似,但是在你调用同步功能之前它永远不会将本地数据同步到Meteor服务器,所以它可能更难用于我们和我们的30个集合:)

希望它对你有所帮助。