暂停方法cordova离子应用程序无法正常工作

问题描述 投票:0回答:1

当我在离子上进入暂停模式时,我试图做一些事情,当暂停和恢复时,日志工作但暂停我要调用一个服务,这将在后端保存应用程序的状态,如果它是我的背景,问题是呼叫需要更多的时间,以便呼叫将永远等待,有没有办法延迟de app离开像ionDidLeav但是对于整个应用程序,或使用角度生命周期,但ngDestroy不起作用,我只需要更多时间,以便呼叫到达后端以保存数据

platform.ready().then(() => {
      if (platform.is('ios')) {
        // document.addEventListener('resign', this.onPause, false);
        // document.addEventListener('active', this.onResume, false);
        document.addEventListener("pause", function() {
          // make a call from service will be pending forever
          console.log("paused")
      }, false);
      document.addEventListener("resume", function() {
        // not firing
        console.log("resumed")
    }, false);
      } else {
        platform.pause.subscribe(() => this.onPause());
        platform.resume.subscribe(() => this.onResume());
      }
    });
angular cordova ionic-framework pause
1个回答
0
投票

我想你可以使用BackgroundMode。链接文档适用于离子4,但如果您使用离子3,则可以找到文档here

只要应用暂停,您就不需要运行后台模式,您可以等到请求完成或出错。请按照有关如何安装它的文档中的步骤进行操作。

import { BackgroundMode } from '@ionic-native/background-mode/ngx';

并将其标记在模块的providers数组中。也可以在组件中导入它并将其注入构造函数,例如backgroundMode

然后只需使用你的pause函数:

document.addEventListener("pause", () => {
  this.backgroundMode.enable(); // enable it!
  this.myService.doSomething().pipe(
    // disable when request completes/errors
    finalize(() => this.backgroundMode.disable()) 
  )
  .subscribe(() => console.log('Yaaaay, successful!'))
}, false);
© www.soinside.com 2019 - 2024. All rights reserved.