簡體   English   中英

使用帶電容器的 Ionic 應用程序為 firebase 實時數據庫啟用離線

[英]Enabling offline for firebase realtime database using Ionic app with capacitor

我正在將離子與 firebase 實時數據庫和電容器 3 一起使用。我打算啟用離線功能。 我已經使用離子帽構建構建了應用程序,然后在 xcode 中打開。 然后按照 url https://firebase.google.com/docs/database/ios/offline-capabilities我在 AppDelegate.Z818056DBD7E201243206B9C7CD888 中添加了以下代碼

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

在 JavaScript 中使用現代 API 時,您需要返回自定義 promise 的情況並不多。

據我所知,您現在擁有以下代碼:

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

可以縮短為:

getSeedConfig(){
  const doc = ref(this.db, 'config/seed');
  return get(doc).then((snapshot) => {
    return snapshot.val()); // 👈 returns null when the snapshot does not exist
  })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM