1

I'm trying to store the total users in the column and see if a user already exists, and if it does it will not make a new object ID, but if it doesn't then it will make a new object ID. in the attachment you'll see that it created multiple object ids even though the user already exists, so instead of updates the scores it makes new ones and never leaves the first part of the if statement since only the object IDs for the row is in there and not the user object ids.

This is my code :

func saveScoresOnParse() {

    var objectUserIdArray = [String]()

    let objectUserIdQuery : PFQuery = PFQuery(className: "Scores")



    objectUserIdQuery.findObjectsInBackgroundWithBlock {

        (objects : [PFObject]? , error : NSError?) -> Void in



        var objectID = objects! as [PFObject]



        for i in 0..<objectID.count {

            objectUserIdArray.append(objectID[i].objectId!)

        }



        for _ in objectID {

            print(objectUserIdArray)

        } 

        for (var i = 0 ; i < objectUserIdArray.count ; i++) {

            if self.userID != objectUserIdArray[i] {

                print("New Scores")



                print("R: \(self.rightAnswers)")

                print("W: \(self.wrongAnswers)")

                print("S: \(self.skippedQuestions)")

                self.scores["User"] = PFUser.currentUser()

                self.scores["Right"] = self.rightAnswers

                self.scores["Wrong"] = self.wrongAnswers

                self.scores["Skipped"] = self.skippedQuestions

                self.scores.saveInBackground()



            } else if self.userID == objectUserIdArray[i] {

                print("Updates Scores")



                self.scores.incrementKey("Right", byAmount: 1)

                self.scores.incrementKey("Wrong", byAmount: 1)

                self.scores.incrementKey("Skipped", byAmount: 1)

                print("R: \(self.rightAnswers)")

                print("W: \(self.wrongAnswers)")

                print("S: \(self.skippedQuestions)")

                self.scores.saveInBackgroundWithBlock {

                (success: Bool, error: NSError?) -> Void in

                if (success) {

                // The score key has been incremented

                } else {

                // There was a problem, check error.description

                }

                }



            } else {

                print("Error")

            }

        }
    }

This is what happens:

Wrong stuff happening

1 Answer 1

1

This is because your declaration of the loop says that it should run while i is greater than objectUserIdArray.count, and you start i at 0. It would be impossible for i to ever be more than objectUserIdArray.count at the beginning. (At most it can be equal to it, i.e. if objectUserIdArray.count was itself 0, but a loop would not be necessary in that case.) Instead make it that the loop runs while i is less than objectUserIdArray.count.

for (var i = 0 ; i < objectUserIdArray.count ; i++) {

In regards to the second problem, since I cannot tell the types of the different variables involved in the checking whether or not the self.userID == objectUserIdArray[i] I cannot be sure that it is the wrong expression, but I think that one that would seem to be better would be:

if objectUserIDArray.contains(self.userID){
    //some code here  
}
else{
    //more code here
}

This snippet should be taken out of the loop that you currently have the comparison in. .contains() checks an array to see if it contains the item that you are looking for and I think it is the function that you are looking for. The current setup will not do anything like this. (This is because you loop through all the possibilities, yet every time you do some sort of action, either creating or editing the user. So if you want to know whether the array contains what you are looking for, and it does, but only at the very end, then you run a lot of code before then that you only mean to run in the event that the array doesn't contain the item, which the code won't know about up until the very end.)

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for that problem! But the main problem still exists, how can store the User column instead of the Object ID column and see if the current user object id already exits? Because now still the 'self.userID' will never equal to that it will keep making new scores and never update the scores.
Is this post has answered your problem, please mark it as correct.
Lol i have but that's not the main problem. The stuff stored in objectUserIDArray are no the object ids in the User column i have in swift. Its the object ids in the Object Id column. That's the main issue I'm having. Because now the first part of the loop always executes.
I think that the array is not initiate correctly, please try Array<String> instead of using [String]() .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.