简体   繁体   English

Ionic 3 - 异步 Http 调用和离子加载问题

[英]Ionic 3 - Async Http Calls and Ionic Loading Issue

I would like to have some help about using Ionic LoadingController while having multiple HTTP calls that are processed in an async function.我想在使用 Ionic LoadingController时获得一些帮助,同时在异步函数中处理多个 HTTP 调用。

Firstly, this is my async function with promises首先,这是我的异步函数与承诺

LoadDatafromUrls(urLs) {
    return new Promise((resolve, reject) => {
      let loader = this.loadingCtrl.create({
        content: 'Son Bilgiler Alınıyor',
      });
      var urlCalls = [];
      urLs.forEach((entry: any) => {
        loader.present().then(()=>{
          this.http.get(entry.url).map((response : Response)=>{
            console.log(response.json());
            urlCalls.push(response.json());
          }).subscribe(()=>{
            console.log("subscribe");
          });
          loader.dismiss();
        })
      })
      Promise.resolve(urlCalls).then((res)=>{
        console.log(res);
        resolve(res);
      },
      (res)=>{
        reject(res);
      })
      //return Promise;
    })
  }

Secondly, I call it in this way.其次,我这样称呼它。

RequestObject.LoadDatafromUrls(urLs).then((results)=>{
        console.log(results);
      },
      (errors)=>{
        console.log(errors);
      })

In this method, I am having the error message below :在这种方法中,我收到以下错误消息:

Error message: Uncaught (in promise): removeView was not found .错误信息: Uncaught (in promise): removeView was not found Moreover, it does not work in the way I am expecting.此外,它不像我期望的那样工作。

I am really open to completely new methods, new ways.我真的很愿意接受全新的方法、新的方式。 Please share if I can have any better implementation.如果我有更好的实现,请分享。

Because you are hiding the loader every time after getting a response from each call.因为您每次在收到每次调用的响应后都会隐藏加载程序。 Ionic throws this error when it tries to dismiss a loader but there is no loader on the view.当 Ionic 尝试关闭加载器但视图上没有加载器时,它会抛出此错误。 Just dismiss the loader only after all calls has been resolved只有在所有调用都得到解决后才关闭加载器

 loader.present();
 urLs.forEach((entry: any) => {
     //   loader.present().then(()=>{
          urlCalls.push(this.http.get(entry.url).map((response : Response)=>{
            console.log(response.json());
            return response.json();
          }).subscribe(()=>{
            console.log("subscribe");
          }).toPromise());
         // loader.dismiss();
        })
     // })
 Promise.all(urlCalls).then((res)=>{
        console.log(res);
        resolve(res);
        loader.dismiss()
      },
      (res)=>{
        reject(res);
      })

EDIT : How to use resolve multiple observables?编辑:如何使用解析多个 observables?

    getData(urls){
     var loader = this.loadingCtrl.create({
       content : "Loading..."
     });
     loader.present();
     this.LoadDatafromUrls(urls).subscribe(res=>{
        console.log(res)//array of responses
        loader.dismiss();
     },err=>{
        console.log(err);// it will fire if any one call fails
        loader.dismiss();
      }); 
    }
    LoadDatafromUrls(urls){
     var arrObservables = [];         
     for(var i = 0; i < urls.length; i++){
         arrObservables.push(this.http.get(urls[i]).map((res: Response)=>{
           return res.json()
         }).catch((err: Response)=>{
           return Observable.throw(err);
         }))
     }
     return Observable.forkJoin(arrObservables);
    }

Remember : Add these import statement in your component :请记住:在您的组件中添加这些导入语句:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/forkJoin'

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

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