在继续休息之前需要完成CloudKit功能

时间:2018-02-04 15:39:22

标签: swift cloudkit dispatch-async completion

我有一个很长的CloudKit函数,它位于for循环中,基本上以返回元组结束。

func loadTuple(members: [String], anotherTeam: Bool) -> [(String, Double)]{
    var temp = [(String, Double)]()
    var tempTuple = [(String, Double)]()
    // make sure it is empty again
    grouplyHours = [Double]()
    if (members.count > 0){
    for x in 0 ..< members.count{

        // loops through each group member
        let pred2 = NSPredicate(format: "username = %@", members[x])
        let query2 = CKQuery(recordType: "PersonalUser", predicate: pred2)
        let operation2 = CKQueryOperation(query: query2)
        //operation.resultsLimit = CKQueryOperationMaximumResults
        operation2.recordFetchedBlock = { (record2: CKRecord!) in

            operation2.qualityOfService = .userInteractive

            if record2 != nil{
                self.userGroupRecordToUpdate = record2

                // takes the groupNames list of the user just accepted

                acceptedUsersArray = (record2.object(forKey: "userGroups") as! Array)


                self.addedUser = members[x]
                grouplyHours = (record2.object(forKey: "hoursExercisedGrouply") as! Array)
                //self.feedTableView.reloadData()
                // tprintw(groupNames.count)

                // not adjusting grouply thingy in time or at all idk, try a different apparoach




        }
        operation2.queryCompletionBlock = { [unowned self] (cursor, error) in
            DispatchQueue.main.async {
                if error == nil {

                    innerLoop: for i in 0 ..< acceptedUsersArray.count{

                        //tprintw("grouplyHours: \(grouplyHours)")
                        // for each person, loops through to find their index that is this group name
                        if acceptedUsersArray[i] == self.groupNameLbl.text{

                            // if you find the index that has the group name, now take the same index of the hours and add it to the tuple

                            // still accessing the first person's grouplyhours array
                            if (members.count - 1 >= x && grouplyHours.count - 1 >= i){
                            tempTuple.append((members[x], grouplyHours[i]))

                            }
                            break innerLoop
                        }
                    }
                    // for loop ends

                    if (anotherTeam == true){
                        temp = tempTuple.sorted(by: {$0.1<$1.1})
                        temp = temp.reversed()


                    }else{
                        sortedTupleArray = tempTuple.sorted(by: {$0.1<$1.1})
                        sortedTupleArray = sortedTupleArray.reversed()
                        if sortedTupleArray.count > 0{ // should be more than 0 since there is always someone in the group

                            self.leadingUserInTeamLbl.text = sortedTupleArray[0].0
                        }

                    }

                  }
                }
            }

        }
    // 7 total

        database.add(operation2)


    // end of loops

   }
}
    addedUser = username

    self.onlyLoadGrouplyHoursForThisGroup()
    if anotherTeam == true{
        return temp
    }
    print("sta: \(sortedTupleArray)")
    return sortedTupleArray
    // put group hours array back to the current user using the app
}

当我说:

时,我在代码中更早地调用了上面的函数
tupleRankings = self.loadTuple(members: groupMembs, anotherTeam: false)
print(tupleRankings) // shows up as empty

我在上面的这一行后面有一条打印行,除了它说元组是空的。同时,我在long函数的最后一行有一个打印行,它打印出一个带有值的元组(函数中的正确响应)。

我需要一种方法来检查在执行打印行之前是否完成了该功能。有没有一种简单的方法可以让你们想出来?

0 个答案:

没有答案