예제 #1
0
        private void setupQueries()
        {
            HashSet <GeoHashQuery> oldQueries = (this.queries == null) ? new HashSet <GeoHashQuery>() : this.queries;
            HashSet <GeoHashQuery> newQueries = GeoHashQuery.queriesAtLocation(center, radius);

            this.queries = newQueries;
            foreach (GeoHashQuery query in oldQueries)
            {
                if (!newQueries.Contains(query))
                {
                    //firebaseQueries[(query)].removeEventListener(this.childEventLister);
                    childEventLister.removeEventListener(GeoUtils.getMapSafe(query, firebaseQueries));
                    firebaseQueries.Remove(query);
                    outstandingQueries.Remove(query);
                }
            }
            foreach (GeoHashQuery query in newQueries)
            {
                if (!oldQueries.Contains(query))
                {
                    outstandingQueries.Add(query);
                    DatabaseReference databaseReference = this.geoFire.getDatabaseReference();
                    Query             firebaseQuery     = databaseReference.OrderByChild("g").StartAt(query.getStartValue()).EndAt(query.getEndValue());
                    childEventLister.addChildEventListener(firebaseQuery);
                    addValueToReadyListener(firebaseQuery, query);
                    //firebaseQueries.Add(query, firebaseQuery);
                    GeoUtils.setMapSafe(query, firebaseQuery, firebaseQueries);
                }
            }
            GeoUtils.foreachSafe(locationInfos.Keys, key => {
                updateLocationInfo(key, GeoUtils.getMapSafe(key, locationInfos).location);
            });
            // remove locations that are not part of the geo query anymore
            List <string> keys = new List <string>(locationInfos.Keys);

            keys.ForEach(k => {
                LocationInfo v = GeoUtils.getMapSafe(k, locationInfos);
                if (!geoHashQueriesContainGeoHash(v.geoHash))
                {
                    locationInfos.Remove(k);
                }
            });
            checkAndFireReady();
        }
예제 #2
0
 private void addValueToReadyListener(Query firebase, GeoHashQuery query)
 {
     ValueChangedListenerSetup vs = new ValueChangedListenerSetup(firebase, true, (e) => {
         lock (Lock) {
             if (e.DatabaseError == null)
             {
                 this.outstandingQueries.Remove(query);
                 this.checkAndFireReady();
             }
             else
             {
                 foreach (GeoQueryEventListener listener in eventListeners)
                 {
                     geoFire.raiseEvent(() => {
                         listener.onGeoQueryError(e.DatabaseError);
                     });
                 }
             }
         }
     });
 }
예제 #3
0
        private void SetupQueries()
        {
            var oldQueries = _queries ?? new HashSet <GeoHashQuery>();
            var newQueries = GeoHashQuery.QueriesAtLocation(_center, _radius);

            _queries = newQueries;
            foreach (var query in oldQueries.Where(query => !newQueries.Contains(query)))
            {
                _firebaseQueries.Remove(query);
                _outstandingQueries.Remove(query);
            }
            foreach (var query in newQueries.Where(query => !oldQueries.Contains(query)))
            {
                _outstandingQueries.Add(query);
                var collection    = _geoFire.GetCollectionRef();
                var firebaseQuery = collection.OrderBy("g").StartAt(query.GetStartValue())
                                    .EndAt(query.GetEndValue());
                _queryListeners.Add(query, firebaseQuery.AddSnapshotListener((snapshot, e) =>
                {
                    if (e != null)
                    {
                        OnError?.Invoke(this, new ErrorEventArgs(e));
                        return;
                    }
                    lock (_lock)
                    {
                        var firQuery = _firebaseQueries.First(x => x.Value.Equals(snapshot.Query)).Key;
                        _outstandingQueries.Remove(firQuery);
                        CheckAndFireReady();
                    }

                    foreach (var change in snapshot.DocumentChanges)
                    {
                        switch (change.Type)
                        {
                        case DocumentChangeType.Added:
                            ChildAdded(change.Document);
                            break;

                        case DocumentChangeType.Removed:
                            ChildRemoved(change.Document);
                            break;

                        case DocumentChangeType.Modified:
                            ChildChanged(change.Document);
                            break;
                        }
                    }
                }));
                _firebaseQueries.Add(query, firebaseQuery);
            }

            foreach (var oldLocationInfo in _locationInfos.Select(info => info.Value).Where(oldLocationInfo => oldLocationInfo != null))
            {
                UpdateLocationInfo(oldLocationInfo.Snapshot, oldLocationInfo.Location);
            }
            // remove locations that are not part of the geo query anymore
            foreach (var entry in _locationInfos.Where(x => !GeoHashQueriesContainGeoHash(x.Value.GeoHash)))
            {
                _locationInfos.Remove(entry.Key);
            }

            CheckAndFireReady();
        }