变量没有被保存在闭包之外

时间:2015-09-04 23:23:34

标签: swift parse-platform closures

我正在使用Parse作为数据库。

问题:我正在查询数据库并保存一个对象,但它似乎没有在查询功能之外保存。我认为这是因为我需要刷新变量或其他东西,但我不知道该怎么做。

相关代码:

class AddClassViewController: UIViewController {

var classroom = Classroom()

var checkClassList: [Classroom] = []

@IBAction func enrollClassButton(sender: AnyObject) {
    self.classroom.classCode = classCodeText.text.uppercaseString
    self.classroom.year = yearText.text
    self.classroom.professor = professorNameText.text

    ClassListParseQueryHelper.checkClass({ (result: [AnyObject]?,error: NSError?) -> Void in
        self.checkClassList = result as? [Classroom] ?? []
        //count is 1 inside here
        println(self.checkClassList.count)
        }, classCode: self.classroom.classCode!)

    //count is 0 out here
    println(self.checkClassList.count)


}

//this gets class with matching classcode
static func checkClass(completionBlock: PFArrayResultBlock, classCode: String){
    let query = PFQuery(className: "Classroom")
    query.whereKey("ClassCode", equalTo: classCode)
    query.findObjectsInBackgroundWithBlock(completionBlock)
}

1 个答案:

答案 0 :(得分:0)

这就是我解决这个问题的方法:

class ClassListParseQueryHelper{
//this gets class with matching classcode
static func checkClass(classCode: String) -> [PFObject]{
    let query = PFQuery(className: "Classroom")
    query.whereKey("ClassCode", equalTo: classCode)
    var test = query.findObjects() as! [PFObject]
    return test
}

self.checkClassList = ClassListParseQueryHelper.checkClass(self.classroom.classCode!) as! [Classroom]