Swift / GeoFire:当没有结果时如何处理

时间:2018-04-05 06:32:53

标签: ios swift firebase geofire

我的应用程序中有一个过滤选项,允许用户更改半径和位置以查看靠近它们的产品,但是当没有结果时我想显示一条消息,但我不知道在哪里放置逻辑处理那个。

代码:

func observeProducts(at: CLLocation, radius: Double, limitTo: UInt, priceMin: Double, priceMax: Double, onSuccess: @escaping (Product_) -> Void, onFailure: @escaping () -> Void) {
    let geoFire = GeoFire(firebaseRef: self.PRODUCTS_GEOFIRE_REF)
    let gfQuery = geoFire.query(at: at, withRadius: radius)

    gfQuery.observe(.keyEntered) { (key, location) in
        if !key.isEmpty {
            self.PRODUCTS_REF.queryLimited(toFirst: limitTo).observe(.childAdded, with: { (snapshot) in
                if let dict = snapshot.value as? [String: Any] {
                    let product = Product_.transform(product: dict, key: snapshot.key)
                    if key == product.productID {
                        if product.price > priceMin && product.price < priceMax {
                            onSuccess(product)
                        }
                    }
                }
            })
        } else {
            onFailure()
        }

    }
}

如何被称为:

func loadProducts(address: CLLocation, radius: Double, limitTo: UInt, priceMin: Double, priceMax: Double, sortBy: @escaping () -> Void) {
    ARSLineProgress.show()

    self.products.removeAll()

    APIs.Products.observeProducts(at: address, radius: radius, limitTo: limitTo, priceMin: priceMin, priceMax: priceMax, onSuccess: { (product) in
        // WORKS FINE...
        self.products.append(product)

        sortBy()

        self.tableView.reloadData()

        ARSLineProgress.hide()
    }) {
            // NOT BEING CALLLED...
            self.errMessage(msg: "Sorry, no products in your area...")
            ARSLineProgress.hide()
    }
}

现在的方式不做任何事情,只是继续显示ARSLineProgress加载视图,但是当有产品时显示产品。我遇到的唯一问题是找到放置onFailure闭包函数的位置。我尝试将它放在if key == product.productID {}区域内,但当半径内没有产品时甚至不会调用它......

0 个答案:

没有答案