Swift iOS应用程序不保存所有对象Parse

时间:2015-04-09 16:18:26

标签: swift parse-platform pfquery pfobject pfuser

我的问题是在用户的数组字段中添加新数字。所有代码似乎都有效,但我遇到了saveAll函数的问题。所有的名字都是正确的,因为我已经在几个不同的场合检查过。

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)

                }

            }

      }

    }

以下内容存储在println(objectQuery)中,因为您可以看到已附加taskID并且tasksCompleted包含字符串。这让我相信saveallinbackground函数存在问题。

[<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;
}]

不确定是否有人对解析api有更多了解,主要是saveall / saveallinbackground函数的工作原理。

1 个答案:

答案 0 :(得分:0)

要以用户身份在后台保存所有内容,我首先要创建一个PFUser变量,例如

var NewUser = PFUser.currentUser

然后保存在后台

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

然后,您可以将if语句和内容放在这些括号中,以便在成功时执行所需的操作。像:

if(success) {
performSegueWithIdentifier("YourSegue", sender: nil)
}