【发布时间】:2019-08-28 15:20:01
【问题描述】:
我正在尝试从 MobX 中的 firestore 同步数据,但我遇到了 onSnapshot 方法的问题。
我正在使用流和生成器来执行此操作。但我不确定如何从 onSnapshot 处理正确的 sub/unsub 监听器
export class MyStore {
@observable myItems: Array<MyModel> = [];
sub: any //??
constructor(){
this.sync();
}
sync = flow(function*(this: MyStore) {
const dbRef = firestore().collection('items');
try {
yield this.sub = dbRef.onSnapshot((data: any)=>{ //??
// do something with data
// const items = ...
this.setItems(items);
})
} catch(error) {
// error handling
}
})
@action setItems = (items: MyModel[]) => {
this.myItems = items;
};
}
以及如何纠正退订? 还有一个问题——只有第一次同步调用才能捕获错误。当我在 firestore 中更改某些内容时,它会更改 myItems,但它会跳过 try catch 范围(它在 onSnapshot 方法的回调中运行 onlny)。如何“尝试/捕捉”firestore 上的每一个变化?
【问题讨论】:
-
请在帖子中只包含一个问题。
标签: google-cloud-firestore synchronization mobx