我是react-native和rnfirebase.io连接到firestore 我的环境如下:
Environment:
OS: macOS High Sierra 10.13.1
Node: 8.9.2
Yarn: 1.5.1
npm: 5.8.0
Watchman: 4.9.0
Xcode: Xcode 9.1 Build version 9B55
Android Studio: 3.1 AI-173.4720617
Packages: (wanted => installed)
react: 16.3.1 => 16.3.1
react-native: 0.55.2 => 0.55.2
我希望我的应用离线工作,我发现Firestore支持默认情况下在https://firebase.google.com/docs/firestore/manage-data/enable-offline离线。
以下是我向Firestore添加行的代码
const db = firebase.firestore().collection('bill');
const data = {
userid: firebase.auth().currentUser.uid,
year: year,
month: month,
description: description,
dateCreated: new Date()
};
db.add(data).then(ref => {
console.log('Added bill with ID: ', ref.id);
}).catch(error => {
console.error("Error adding bill: ", error);
});
当有互联网连接时,一切顺利。当我关闭互联网连接并尝试添加记录时,就像要永远添加一样。
我匿名验证我的用户,如下所示
componentWillMount() {
const user = firebase.auth().currentUser;
if (!user) {
firebase.auth()
.signInAnonymouslyAndRetrieveData()
.then(credential => {
if (credential) {
console.log('default app user ->', credential.user.toJSON());
} else {
console.log('failed to auth anonymous user');
}
});
}
}