简体   繁体   English

将来自 firebase 的数据存储到 firebase 离线存储与 ionic 5

[英]store data from firebase into firebase offline storage with ionic 5

I am new in ionic with angular and firebase.我是 angular 和 firebase 的离子新手。 So I want to store my firebase data list in a cache so that the page can load once because currently I have a lot of data and it slows down the app when the user enters the page each time.所以我想将我的 firebase 数据列表存储在缓存中,以便页面可以加载一次,因为目前我有很多数据,并且每次用户进入页面时都会减慢应用程序的速度。 please how can I handle my problem?请问我该如何处理我的问题?

that is my model那是我的 model

    import { Media } from './Media';
    export class Post {
        id?: string;
        icone: string;
        owner?: string;
        titre?: string;
        description?: string;
        medias?: Array<Media> = new Array();
        mediasThumbnail?: Array<Media> = new Array();
        abonnees?: Array<string> = new Array();
        typePost?: string; // Formation, Projet, ...
        refSrc?: string;
        date?;
        location?: string;
        userSaved: string[] = new Array();
        userDel: string[] = new Array();
        debut;
        fin;
        breComment: number;enter code here
        descriptioncourte: string;
        competences?: string[];
        userliked?: string[] = new Array();
        userviewed?: string[] = new Array();
        }

that is my service那是我的服务


    import { Injectable } from '@angular/core';
        import * as firebase from 'firebase';
        import { Post } from '../models/Post';
        import {
          AngularFirestoreCollection,
          AngularFirestore,
        } from "@angular/fire/firestore";
        @Injectable({
          providedIn: 'root'
        })
        export class PostService {
          db: firebase.firestore.Firestore;
          readonly path = "posts";
          constructor(private afs: AngularFirestore) {
            this.db = firebase.firestore();
          }
        
        
          findByfilters(keys: string[], operator: firebase.firestore.WhereFilterOp[], values: any[], orderby?: string): firebase.firestore.CollectionReference {
        
            let query;
            let i = 0;
            keys.forEach(s => {
                if (!query) {
                    query = this.db.collection(this.path).where(s, operator[i], values[i]);
                } else {
                    query = query.where(s, operator[i], values[i]);
                }
        
                i++;
            });
            if (!query) {
                query = this.db.collection(this.path);
            }
            if (orderby) {
                return query.orderBy(orderby);
            }
            return query;
        }
        
         }
    
   

and then that is how I list my data然后这就是我列出数据的方式

class MyPage {
  constructor(private postSvc: PostService) {
      this.postSvc.
      findByfilters(this.keys, 
        this.operator, 
        this.values).onSnapshot((data) => {
        this.postitems = new Array<Post>();
        data.forEach(async (item) => {
          const post = item.data() as Post;
          post.id = item.id;
          this.postitems.push(post);
        })
      });
  }
}

Please can somebody help me to show how can I implement my service to save my data in firebase offline?请有人帮我展示如何实现我的服务以离线保存我的数据在 firebase 中? thanks.谢谢。

AngularFire provides a built-in feature for offline persistence . AngularFire 为离线持久性提供了内置功能 This will be completely transparent for you, db.collection() will simply return what AngularFire did store in its cache when there is no connection available.这对你来说是完全透明的,当没有可用的连接时, db.collection()将简单地返回 AngularFire 在其缓存中存储的内容。

To enable it you should simply add AngularFirestoreModule.enablePersistence() in your module (probably app.module.ts ).要启用它,您只需在模块中添加AngularFirestoreModule.enablePersistence() (可能是app.module.ts )。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { environment } from '../environments/environment';

@NgModule({
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule.enablePersistence(), // <--- HERE
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM