Geofire - 向 GFRegionQuery 添加监听器

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

有没有办法在 .observeReady 运行后收听添加到 GFRegionQuery 的新项目?

我创建一个 regionQuery:GFRegionQuery? 作为类属性。我使用当前用户的位置和任何半径来设置它。在我找到该半径内的所有餐厅并使用 regionQuery?.observeReady 完成后,我想听听后来在该用户的位置和半径内添加的任何新餐厅。

例如,最初使用获取代码提取 18 家餐厅,之后使用邮政编码添加 1 家或更多餐厅。我希望获取代码能够收听并拉取新餐厅,假设它们在同一用户区域内。

获取代码:

let geoDBRef = Database.database().reference().child("geo")
var regionQuery: GFRegionQuery?
var queryHandle: UInt?
var fetchedGeoModels = [GeoModel]()
var listenerGeoModels = [GeoModel]()

func fetchNearbyRestaurants(within radius: Double) {

    guard let currentUserLocation = locationManager.location else { return }

    let region = MKCoordinateRegion(center: currentUserLocation.coordinate, latitudinalMeters: radius, longitudinalMeters: radius)

    let geoFire = GeoFire(firebaseRef: geoDBRef)
    regionQuery = geoFire.query(with: region)

    queryHandle = regionQuery?.observe(.keyEntered, with: { [weak self](key: String!, location: CLLocation!) in
        let geoModel = Model(postId: key, location: location)  
        self?.fetchedGeoModels.append(geoModel)
        // 18 geoModels in total are fetched
    })

    regionQuery?.observeReady({ [weak self] in
        self?.regionQuery?.removeObserver(withFirebaseHandle: queryHandle!)

        // 1. I sort the fetchedGeoModels by postId from newest to oldest
        // 2. I fetch all 18 posts from postsRef using the postIds from the fetchedGeoModels

        // 3. *** The listener should possibly start here, and when anything else is added within the region, append it to the listenerGeoModels. Once I get those I'll fetch each post using the postId ***
    })
}

邮编:

guard let postId = Database.database().reference().child("posts").childByAutoId().key else { return }
    
let restaurantLocation = CLLocation(latitude: lat, longitude: lon)
let geoDBRef = Database.database().reference().child("geo")
let geoRef = GeoFire(firebaseRef: geoDBRef)
geoRef.setLocation(restaurantLocation, forKey: postId) { ... }
    
let postsRef = // set with the postId as key and restaurant name as k/v

架构

geo
 -MWqjt5u0T_Ipk0Wadvh
   -g
   -l
    -0:23.1234
    -1:-78.7890
posts
 -MWqjt5u0T_Ipk0Wadvh
    "name": "Patsy's Pizzeria"
ios swift firebase firebase-realtime-database geofire
© www.soinside.com 2019 - 2024. All rights reserved.