2017-02-18 4 views
0

Imonic 2 appをビルドして、アプリケーションが読み込まれると、あなたの近くのユーザーが表示されるはずですが、重複した値を表示しますが、これは最初の値が表示され、次に何が来るのかということで、動作の主題は?すべてのロードされた後に値を表示する方法はありますか?私の頭の中では、それがいっぱいになる配列のように動作して、それだけが表示されるか、エントリがいっぱいになってしまうでしょう。RXJS Behavior主題重複を最初の負荷で出力する

ここ

は、次の

import { Injectable, NgZone } from '@angular/core'; 
import { Geolocation, Geoposition, BackgroundGeolocation } from 'ionic-native'; 
import { Fb } from './firebase'; 
import { Observable } from 'rxjs/Observable'; 
import {Subject} from 'rxjs/Subject'; 
import {BehaviorSubject} from 'rxjs/BehaviorSubject'; 
import { User } from './user'; 
import 'rxjs/add/operator/filter'; 
import 'rxjs/add/operator/share'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/Rx'; 

declare var GeoFire: any 


@Injectable() 
export class LocationTracker { 

public firebaseRef: any; 
public geoFire: any; 
public subject: Subject<any> = new BehaviorSubject(0); 

constructor(public zone: NgZone, public fire:Fb, public user:User) { 
    this.firebaseRef = this.fb.ref('geofire'); 
    this.geoFire  = new GeoFire(this.firebaseRef); 
} 

UsersNearME() : Observable<any> { 
    this.nearme.length = 0; 
    let geoQuery = this.geoFire.query({ 
    center: [this.lat, this.lng], 
    radius: 4.609 //kilometers 
    }); 
    geoQuery.on("key_entered", (key, location, distance) => { 

const sender = this.fb.ref('/users/'+ key ); 
    sender.on('value', (snapshot) => { 
     const profile = snapshot.val().profile; 
     this.nearme.push({ 
      userid: key, 
      userloc: location, 
     }); 
     this.subject.next(this.nearme); 
     // 
    }); 
    }); 
return this.subject.asObservable(); 
} 
} 

マイComponent.tsは、以下のようで、基本的にBehaviorSubjectが何であるかであるユーザーサービス

import { Component, NgZone, ChangeDetectorRef, Inject, OnInit } from '@angular/core'; 
    import 'rxjs/Rx'; 

    @Component({ 
    selector: 'members', 
    templateUrl: 'members.html' 
    }) 
    export class MembersOverviewPage implements OnInit { 

    public usersNearMe; 

    constructor(locationTracker: LocationTracker, 
       zone: NgZone,) { 
    } 

     ngOnInit() { 
       this.zone.run(() => { 
        setTimeout(() => { 
         try { 
          this.locationTracker.UsersNearME().filter(data => data != null).subscribe(data => { 
          if(data) { 
            this.usersNearMe = data; 
            this.loader.dismiss(); 
           } 
          }, (error) => { 
           alert("are you online ? "); 
          },() => { 
          // this.loader.dismiss(); 
          }); 

         } catch (e) { 
          console.log(" error "+ JSON.stringify(e)); 
         }       
        }, 1000); 
       }); 
     } 
    } 
+0

「this.nearme」という配列をどのように作成するかが問題です。あなたはそれにユーザーを押し込んだ後、あなたは出す。あなたは別の値をプッシュし、次にあなたは放出します。近くのユーザー**をすべて取得してから**アレイを一度に放出するほうがよいでしょうか? – AngularChef

答えて

3

にサブスクライブを使用してコンポーネントにユーザーをプッシュしている私のService.tsですそれは、まずそこにある価値を与えます、その後はすべての次の値。最初の価値を望まない場合は、単純にSubjectに変更してください。

public subject: Subject<any> = new Subject(); 

たぶんReplaySubjectもあなたのために働くことができます:

http://xgrommx.github.io/rx-book/content/subjects/replay_subject/index.html

UPDATE:私はこのコードを理解したよう

this.nearme.push({ 
     userid: key, 
     userloc: location, 
    }); 
    this.subject.next(this.nearme); 

、あなたが新しい値で配列をインクリメントしたいです全体の配列は再び加入者に表示されますか?そうでない場合、

this.subject.next({ 
     userid: key, 
     userloc: location, 
    }); 
+0

次に、最後の値を得ることはできません – Kravitz

+0

@REPTILEバッファ長1のreplaysubjectを見ましたか? – laser

+0

いいえ、しかし今私は – Kravitz

関連する問題