EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 448 forks source link

How to make a last write when app exit? #521

Open gbonline opened 6 years ago

gbonline commented 6 years ago

Hi, I'm looking for a way to make a last write to firebase db, when user exit the app ( or suspend). I read the Application Events page and wrote some code. I'm trying putting the event listeners on the AppComponent level, where I can access to the service that exposes the methods for firebase, as show below:

import { UserDataService } from './shared/user/user-data.service';
import { Page } from 'ui/page';
import {DeviceType, DeviceOrientation} from 'ui/enums';
import { Component } from "@angular/core";
import * as Platform from 'platform';
import { TranslateService } from '@ngx-translate/core'; 
import { TNSFontIconService } from 'nativescript-ngx-fonticon'; 
import app = require("application");

@Component({
  selector: "main",
  template: `
       <page-router-outlet></page-router-outlet>
  `
})
export class AppComponent {
  constructor(private translate: TranslateService, private fonticon: TNSFontIconService, 
              private dataService: UserDataService ) {
    this.translate.setDefaultLang('en');
    let master_lang = Platform.device.language.slice(0,2);
    this.translate.use(master_lang);

    app.on(app.exitEvent, this.callUnset );
    app.on(app.suspendEvent, this.callUnset );  
    app.on(app.resumeEvent, this.callSetLogin );
  }

  public changeLanguage(lang: string) {
    this.translate.use(lang.slice(0,2));
  }

  callUnset() {
    console.log("CALL EXIT/SUSPEND EVENT");
    this.dataService.unsetLoggedInFirebase();
  }

  callSetLogin() {
    console.log("CALL RESUME EVENT");
    this.dataService.setLoggedInFirebase();
  }
}

When the event "Exit" fire, the callUnset() method is called, but the object this.dataService is undefined !! I ask you kindly, if anyone can tell me how to make a last write on firebase before closing the app?

richarddavenport commented 6 years ago

I'm thinking that you are running into an angular issue. The angular app is probably being destroyed when that event is fired and the app component might be the last thing destroyed since it one of the top most components. You'll probably need to write it outside of angular somehow. It might work if you put it in the main.ts file.

gbonline commented 6 years ago

Thanks for aswer

it's the same thing I thought.

But I used a service to collect all the methods to use the firebase plugin , and I think this service is currently unavailable at main.ts,

I'll make some investigation to know which object I can have at main.ts level.

regards

Giorgio

Il 23 ottobre 2017 alle 12.46 Richard Davenport notifications@github.com ha scritto:

I'm thinking that you are running into an angular issue. The angular app is probably being destroyed when that event is fired and the app component might be the last thing destroyed since it one of the top most components. You'll probably need to write it outside of angular somehow. It might work if you put it in the main.ts file.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/521#issuecomment-338620626 , or mute the thread https://github.com/notifications/unsubscribe-auth/ADH0kfvvGy_xjydqP0XuycQRLwRR-Nd2ks5svG6WgaJpZM4P6-ZN .
dbeulen commented 6 years ago

Just for your reference, firebase has a function to do this server side (so even when a client loses power, internet, or whatever), the online flag will reset.

The only drawback: it is not (yet) implemented in this plugin. It's on my TODO list, but unfortunetely i do not think i will have time to look into this anytime soon. :(

See #500 for more information.