在swift中查询私有数据库以获取用户详细信息

时间:2016-02-29 02:33:25

标签: swift swift2 cloudkit

我尝试保存并存储用户的数据,然后检索并查看值,推送相应的视图控制器。

然而,尽管我的cloudkit仪表板中只有4个用户记录,但我得到了33个结果,迫使我改变我的代码并阻止它工作。

这是我的原始代码:

    let container = CKContainer.defaultContainer()
    let privateDB = container.privateCloudDatabase
    let resultPredicate = NSPredicate(format: "TRUEPREDICATE")
    let query = CKQuery(recordType: "UserData", predicate: resultPredicate)
    query.sortDescriptors = [NSSortDescriptor(key: "MODIFIED", ascending: false)]

    privateDB.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in

        if error != nil {
            print("\(error)")
        }
        else{

       for record in results! {

                self.weight = record["weight"] as? Int
                self.height = record["height"] as? Int
                self.age = record["age"] as? Int
                self.gender = record["genderFemale"] as? Int

                if self.weight == nil {
                    print("push weightVC")
                    let weightVC = WeightViewController()
                    self.navigationController?.pushViewController(weightVC, animated: false)
                }

                else if self.height == nil {
                    print("push heightVC")
                    let heightVC = HeightViewController()
                    self.navigationController?.pushViewController(heightVC, animated: false)
                }

                else if self.age == nil {
                    print("push ageVC")
                    let ageVC = DOBViewController()
                    self.navigationController?.pushViewController(ageVC, animated: false)
                }

                else if self.gender == nil{
                    print("push genderVC")
                    let genderVC = GenderViewController()
                    self.navigationController?.pushViewController(genderVC, animated: false)
                }
                else{
                    let planVC = PlanOriginViewController()
                    self.navigationController?.pushViewController(planVC, animated: false)
                }

我被迫将其更改为:

      privateDB.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in

        if error != nil {
            print("\(error)")
        }
        else{

            print("\(results?.count)")

            for record in results! {

                self.weight = record["weight"] as? Int
                self.height = record["height"] as? Int
                self.age = record["age"] as? Int
                self.gender = record["genderFemale"] as? Int

                self.arrayOfUserData?.addObject(record)
                print("record added")
            }

        }
    }

    print(arrayOfUserData)

    if arrayOfUserData != nil{
    let ckRecord = arrayOfUserData![0]

    self.weight = ckRecord["weight"] as? Int
    self.height = ckRecord["height"] as? Int
    self.age = ckRecord["age"] as? Int
    self.gender = ckRecord["genderFemale"] as? Int

    if self.weight == nil {
        print("push weightVC")
        let weightVC = WeightViewController()
        self.navigationController?.pushViewController(weightVC, animated: false)
    }

    else if self.height == nil {
        print("push heightVC")
        let heightVC = HeightViewController()
        self.navigationController?.pushViewController(heightVC, animated: false)
    }

    else if self.age == nil {
        print("push ageVC")
        let ageVC = DOBViewController()
        self.navigationController?.pushViewController(ageVC, animated: false)
    }

    else if self.gender == nil{
        print("push genderVC")
        let genderVC = GenderViewController()
        self.navigationController?.pushViewController(genderVC, animated: false)
    }
    else{
        let planVC = PlanOriginViewController()
        self.navigationController?.pushViewController(planVC, animated: false)
    }

    } else {

    }

但是,这也不起作用。在返回privateDB查询之前,XCode正在跳过privateDB查询块并直接进入行print(arrayOfUserData)。

谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题是您假设您的文件是单线程的。您希望它评估的内容一旦您收到数据库查询的结果,哪些字段未填写。你需要的是一个完成块。这使您可以查询数据库,然后评估结果。我会围绕这些界限做点什么。

首先添加一个完成处理程序,它会返回一个用户对象。我假设你有一个用户定义应该看起来像这样

struct User{
     weight:Int
     height:Int
     age:Int
     gender:Sting
 }

在查询方法中添加完成处理程序。

privateDB.performQuery(query, inZoneWithID: nil, completion:([User])) { (results, error) -> Void in

   let resutltData = results.value as [String:AnyObject] ?? [:]
       let users : [User] = resutltData.flatMap { record in
            let weight = record["weight"] as? Int ?? ""
            let height = record["height"] as? Int ?? ""
            let age = record["age"] as? Int ?? ""
            let gender = record["gender"] as? String ?? ""
       }
       completion(user)
}

现在,当您致电privateDB.performQuery时,您应将其保存在变量中并在那里执行检查。像这样:

func ProcessUsers(){
    let users = privateDB.performQuery(query, inZoneWithID: nil, completion:{ (User) in 
    self.user = User
    switch user{
        case .weight:
            if .weight == "" {
                print("push weightVC")
                let weightVC = WeightViewController()
                self.navigationController?.pushViewController(weightVC, animated: false) } else {
                  fallthrough
               }

        case .height:
            if .height == "" {
                print("push heightVC")
                let heightVC = heightViewController()
                self.navigationController?.pushViewController(heightVC, animated: false) } else {
                  fallthrough
               }
        case .age:
            if .age == "" {
                print("push ageVC")
                let ageVC = ageViewController()
                self.navigationController?.pushViewController(ageVC, animated: false) } else {
                  fallthrough
               }
        case .gender:
            if .gender == "" {
                print("push genderVC")
                let genderVC = genderViewController()
                self.navigationController?.pushViewController(genderVC, animated: false) } else {
                  fallthrough
               }

如果您对此有任何疑问,请与我们联系。我希望它有所帮助。