西瓜同步Firebase



我想使用西瓜同步与Firestore,但我没有得到它。我甚至不知道从何说起。我没有使用API。我只想在React Native中做。我想同步我的应用程序离线和在线。有人能帮帮我吗?我使用React Native来做这个…

import { synchronize } from '@nozbe/watermelondb/sync'
async function mySync() {
await synchronize({
database,
pullChanges: async ({ lastPulledAt, schemaVersion, migration }) => {
const urlParams = `last_pulled_at=${lastPulledAt}&schema_version=${schemaVersion}&migration=${encodeURIComponent(JSON.stringify(migration))}`
const response = await fetch(`https://my.backend/sync?${urlParams}`)
if (!response.ok) {
throw new Error(await response.text())
}
const { changes, timestamp } = await response.json()
return { changes, timestamp }
},
pushChanges: async ({ changes, lastPulledAt }) => {
const response = await fetch(`https://my.backend/sync?last_pulled_at=${lastPulledAt}`, {
method: 'POST',
body: JSON.stringify(changes)
})
if (!response.ok) {
throw new Error(await response.text())
}
},
migrationsEnabledAtVersion: 1,
})
}
上面的例子是西瓜网站上显示的代码。但是我想在不使用API的情况下做到这一点!只支持React Native和Firestore/firebase。我如何在react Native中做到这一点,每当应用程序中有任何变化时,当用户连接到互联网时,它会自动将其保存在数据库中?我的应用程序是离线优先

有一些包可以帮助您将react原生同步到firebase,您不需要构建自己的后端服务器和api。从西瓜文档中引用。

MelonFire,一个React Native库来同步你的数据库到Firestore。MelonFire克服了实现中的常见错误(例如时间戳抖动,多个写入器,Firestore的500写事务限制,重试)以保证数据库一致性。

fireelon,一个将数据库同步到Firestore的替代实现。它依赖于小于Firestore的500个写事务限制的更改,并且不处理服务器时间戳的复杂性,但支持在备份时忽略某些表。

希望对你有帮助。

最新更新