PFQuery完成后启动一些东西

时间:2016-01-06 02:44:00

标签: ios swift parse-platform pfquery

查询完成后是否有可能做某事?我有一个查询对象的PFQuery,这些对象的objectIds将依赖于一个Array。 materUserObjects是一个PFUser,MatesIdsAndNames是一个字典,其余的是String`func queryNow(){

    if mateUserObject.isEmpty == false{

        for mateId in mateUserObject{



            let date = NSDate()
            let dateInt = date.timeIntervalSince1970

            let query = PFQuery(className: "calendarEvents")
            query.whereKey("createdBy", equalTo: mateId)
            query.whereKey("StartDate", lessThan: dateInt)
            query.whereKey("EndDate", greaterThan: dateInt)
            query.cachePolicy = .NetworkOnly
            query.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in

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

                    if objects?.isEmpty == true{
                        self.freeMateIds.append(mateId.objectId!)
                        print(self.freeMateIds)
                        if self.mateUserObject.last == mateId{


                            if self.freeMateIds.isEmpty == false{


                                for freeMate in self.freeMateIds{

                                    let oneFreeMate = self.matesIdsAndNames[freeMate]
                                    self.freeMateNames.append(oneFreeMate!)
                                    print(self.freeMateNames)
                                }

                            }
                            self.MatesTableView.reloadData()

                        }

                    }

                }


            })



        }


    }


}`

1 个答案:

答案 0 :(得分:0)

你使用query.findObjectInBackgroundWithBlock吗?如果是,我建议您使用竞争处理程序:

typealias CompletionHandler = (success: Bool) -> Void

//  Ajoute dans le tableau _applications les applications stockées sur Parse
func fetchData(completionHandler: CompletionHandler) {
    let query = PFQuery(className: "Test")
    var supposeObjectCount: Int = 0
    var objectCount: Int  = 0

    //  Ajoute un filtre pour la requete
    query.orderByAscending("Id")

    //  Trouve les objets sur Parse
    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in

        //  S'il n'y a pas d'erreur
        if error == nil {

            supposeObjectCount = objects!.count


            if let objects = objects {


                for object in objects {

                    // ..

                    if(supposeObjectCount == ++objectCount) {
                        completionHandler(success: true)
                    }
                }

            }
        }

        else {
            print("Error")
        }
    }
}



//  Call the function

func test() {
    fetchData({ (success) -> Void in


        if success {
            //  Executed when query is completed
        }
    })
}

希望这有帮助!