【发布时间】:2022-02-01 06:08:57
【问题描述】:
我正在使用带有 firebase 实时数据库和电容器 3 的 ionic。我打算启用离线功能。我已经使用 ionic cap build 构建了应用程序,然后在 xcode 中打开。然后跟随 url https://firebase.google.com/docs/database/ios/offline-capabilities 我在 AppDelegate.swift 中添加了以下代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
return true
}
现在为了测试我在 wifi 上运行应用程序并从 firebase db 获取数据。在此之后,我杀死了应用程序并关闭了 wifi。但是,在启动应用程序时,它不会加载数据。
我还有什么遗漏的吗?
我的关键 pod 文件有:
target 'App' do
capacitor_pods
# Add your Pods here
pod 'FirebaseCore', '7.11.0' # Add this line
pod 'Firebase/Database', '7.11.0' # Add this line
end
下面是我的代码,它不起作用,预计会:
getSeedConfig(){
return new Promise((resolve, reject) =>{
const doc = ref(this.db, 'config/seed');
get(doc).then((snapshot) => {
if (snapshot.exists()) {
resolve(snapshot.val())
} else {
resolve(null)
}
}).catch((error) => {
reject(error)
});
})
}
【问题讨论】:
-
@FrankvanPuffelen 我已经添加了示例代码
-
嗯……乍一看还不错,尽管从 observable 中获取第一个元素是一种相当复杂的方式。 Calling
first()应该做同样的事情(iirc)。 -
那么,这里有什么建议?
-
嗯...我建议在这里使用
first(),甚至是不带rxjs 的getoronce。 --- 但是在我们当前的代码中,您的subscribe回调是否曾经在离线时被调用过? -
所以我修改了代码 not yes 离线测试,因为我不确定这段代码是否适合您使用基于文档的 get?
标签: javascript ios firebase firebase-realtime-database