0

I have this long CloudKit function that is inside for loops which basically ends in returning a tuple.

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
}

I call the above function earlier in my code when I say:

tupleRankings = self.loadTuple(members: groupMembs, anotherTeam: false)
print(tupleRankings) // shows up as empty

I have a print line directly after this line above, except it says the tuple is empty. Meanwhile, I have a print line in the last line of the long function and it prints out a tuple with values (correct response in function).

I need a way to check if I am done completing the function before I execute the print line. Is there an easy way you guys can come up with?

jscs
  • 63,694
  • 13
  • 151
  • 195
Andy L
  • 1

0 Answers0