0

My problem is adding new numbers to the array field in users. all the code seems to work but i have the problem with saveAll function. All the names are correct, as i've checked on several different occasions.

func taskAllocation(){
        var query = PFUser.query()
        var objectQuery:[PFUser] = [PFUser]()
        query.whereKey("Year", equalTo: yearTextField.text.toInt())
        query.whereKey("Class", equalTo: classTextField.text.toInt())
        query.whereKey("Keystage", equalTo: keystageTextField.text.toInt())

    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in

        if error != nil {
            println(error)
        } else {
            if objects.isEmpty {
                println("empty query")
            } 
            else {

                for object in objects as [PFUser]  {

                    var test:[Int] = object["taskIDs"] as [Int]
                    test.append(self.getIndex)
                    println(test)

                object["taskIDs"] = test
                object["tasksCompleted"] = "hello"
                objectQuery.append(object)


                }//For


                println(objectQuery.count)
                if objectQuery.count != 0{
                    println("Success!!!")
                    println(objectQuery)
                    PFUser.saveAllInBackground(objectQuery)

                }

            }

      }

    }

This below is stored in the println(objectQuery) as you can see the taskIDs have been appended and the tasksCompleted contains the string. Which leads me to believe that there is a problem with the saveallinbackground function.

[<PFUser: 0x155f6510, objectId: W6TrPQwCQ7, localId: (null)> {
    Class = 2;
    Forename = "Joe ";
    Keystage = 2;
    Surname = Freeston;
    Year = 4;
    admin = false;
    completedTaskIDs =     (
    );
    taskIDs =     (
        0,
        2,
        4,
        5,
        6,
        46
    );
    tasksCompleted = hello;
    tasksCorrect = 0;
    userType = Student;
    username = jfreeston;
}, <PFUser: 0x1569b7a0, objectId: slLd1KBIaM, localId: (null)> {
    Class = 2;
    Forename = "Camilla ";
    Keystage = 2;
    Surname = Linhart;
    Year = 4;
    admin = false;
    completedTaskIDs =     (
    );
    taskIDs =     (
        0,
        46
    );
    tasksCompleted = hello;
    tasksCorrect = 0;
    userType = Student;
    username = clinhart;
}]

Not sure if anyone has more knowledge of the parse api, mainly how the saveall/saveallinbackground functions work maybe.

TomTom
  • 899
  • 8
  • 16

1 Answers1

0

To save all in background as a user I would first create a PFUser variable, like

var NewUser = PFUser.currentUser

Then to save in background

newUser.saveInBackgroundWithBlock {
(success:Bool, error: NSError!) -> Void in {
}

Then you can put if-statements and stuff within those brackets to do what you need it to when successful. like:

if(success) {
performSegueWithIdentifier("YourSegue", sender: nil)
}
JCDOSAJ
  • 119
  • 1
  • 5
  • This isn't for currentUser, but all users that match the queries. As the current user would be an administrator adding a new tasksID to users that match with this result – TomTom Apr 09 '15 at 16:52