首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GeoFire查询完成- Swift

GeoFire查询完成- Swift
EN

Stack Overflow用户
提问于 2017-05-20 11:59:02
回答 1查看 358关注 0票数 0

在我的应用程序中,我使用GeoFire查询用户的位置。这是一个类似丁丁的应用程序,有卡片等等.我用KolodaView做卡片。

查询功能:

代码语言:javascript
复制
func queryUsers(searchRadius: Int, center: CLLocation) {

    print("Query started")

    let geofireRef = Database.database().reference().child("usersLocations")
    let geoFire = GeoFire(firebaseRef: geofireRef)

    let circleQuery = geoFire?.query(at: center, withRadius: Double(searchRadius))

    _ = circleQuery?.observe(.keyEntered, with: { (result, location) in

        print("User \(result!) found")
        print("Adding user \(result!)")

        addUser(userID: result!, completionHandler: { (success) in

            print("User added")

        })

    })

}

添加用户功能:

代码语言:javascript
复制
func addUser(userID: String, completionHandler:@escaping (Bool) -> ()) {

    let foundUsers = UserDefaults.standard.array(forKey: "foundUsers")

    let databaseRef = Database.database().reference()

    databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in

        if (foundUsers?.count)! < 20 {

            // USERS

            let users = (snapshot.value as? NSDictionary)?["users"] as! NSDictionary

            // USER

            let user = users[userID] as! NSDictionary

            getFirstUserPicture(userID: userID, completionHandler: { (data) in

                if let data = data {

                    DispatchQueue.main.async {

                        user.setValue(data, forKey: "picture")

                        // APPEND FOUND USERS ARRAY

                        var foundUsers = UserDefaults.standard.array(forKey: "foundUsers")

                        foundUsers?.append(user)

                        // STORE FOUND USERS ARRAY

                        UserDefaults.standard.set(foundUsers, forKey: "foundUsers")
                        UserDefaults.standard.synchronize()

                        // UPDATE CARD VIEW

                        if foundUsers?.count == 1 {

                            NotificationCenter.default.post(name: .loadCardView, object: nil)

                        } else {

                            NotificationCenter.default.post(name: .loadCardsInArray, object: nil)

                        }

                        // COMPLETION

                        completionHandler(true)

                    }

                }

            })

        }

    }) { (error) in

        print(error.localizedDescription)

    }

}

启动应用程序时,将调用queryUsers函数,查询将启动

输出

代码语言:javascript
复制
User 1XXXXXXXXXXXXXXXX found
Adding user 1XXXXXXXXXXXXXXXX
User 2XXXXXXXXXXXXXXXX found
Adding user 2XXXXXXXXXXXXXXXX
User added
User added
  1. 找到用户
  2. 添加用户(调用addUser函数)

问题是,它没有等待addUser完成来为找到的第二个用户调用addUser。结果是,在我的KolodaView中,第二个用户被发现了两次,因为我认为addUser对找到的第二个用户的调用使用了第一个用户找到的参数。

是否可以等待第一个addUser完成并重新启动查询?还是在找到第一个用户之后“暂停”查询,并在第一个addUser调用完成后再次启动它?

谢谢你的帮忙

更新1

我尝试了@Rlee128 128解决方案,但是没有什么改变,我有相同的输出:(:

代码语言:javascript
复制
// MARK: UPDATE USER LOCATION - FUNCTION

func updateUserLocation() {

    let databaseRef = Database.database().reference().child("usersLocations")
    let geoFire = GeoFire(firebaseRef: databaseRef)

    let userID = UserDefaults.standard.string(forKey: "userID")!

    let locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.startUpdatingLocation()

    geoFire?.setLocation(locationManager.location, forKey: userID, withCompletionBlock: { (error) in

        if error != nil {

            print(error as Any)

        } else {

            // Location updated

        }

    })

}


// MARK: LOCATION MANAGER DELEGATE - FUNCTION

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    manager.stopUpdatingLocation()
    manager.delegate = nil

}
EN

回答 1

Stack Overflow用户

发布于 2017-05-24 21:58:43

在location manger didUpdateLocations中,在调用manager.stopUpdatingLocation()之后,将manager.stopUpdatingLocation设置为=0。如果你想让我在我的代码中向你展示它的外观,请告诉我。

代码语言:javascript
复制
func findLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations[0] as CLLocation
    manager.stopUpdatingLocation()
    manager.delegate = nil
    let loc = CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    getLocation(location: loc)

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44085598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档