Parse PFQuery返回nil(swift)

时间:2015-05-22 20:02:10

标签: ios swift parse-platform null pfquery

我正在尝试添加一个我创建的名为“Party”的类的数组,在UITableView中使用,但我创建的函数返回数组的nil值,即使在Parse中有一个。

我在视图控制器中创建了这样的数组:

private var parties:[Party] = [Party]()

我也尝试了同样的结果:

var parties:[Party] = [Party]()

该功能如下所示:

func retrieveParties() {
    //create new PFQuery
    var query:PFQuery = PFQuery(className: "Party")

    //call findobjectinbackground
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in

        //clear parties
        self.parties = [Party]()

        //loop through the objects array
        for partyObject in objects! {
            //retrive the Text column value for each PFobject
            let party:Party? = (PFObject: partyObject as? Party)
            //Assign it to our parties
            if party != nil{
            self.parties.append(party!)
            }
        }

        dispatch_async(dispatch_get_main_queue()){
            //reload the table view
            self.discoverTableView.reloadData()
        }
    }
}

我也试过这个:

func getParties() {

    // Clear up the array
    parties.removeAll(keepCapacity: true)
    self.discoverTableView.reloadData()

    // Pull data from Parse
    let query = PFQuery(className:"Party")
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error: NSError?) -> Void in

    if error == nil {
    if let objects = objects as? [PFObject] {
    for (index, object) in enumerate(objects) {
        // Convert PFObject into Party object
        let party = Party(pfObject: object)
        self.parties.append(party)
        }
    }

} else {
    // Log details of the failure 
    println("Error: \(error) \(error!.userInfo)")
    }
}
    //query.cachePolicy = PFCachePolicy.NetworkElseCache
}

无论我使用哪个函数,数组都是空的。非常感谢能够解决这个问题的人!

1 个答案:

答案 0 :(得分:2)

哦,我刚解决了这个问题!我所做的只是将getParties()函数更改为:

func getParties() {

    // Clear up the array
    parties.removeAll(keepCapacity: true)
    self.discoverTableView.reloadData()

    // Pull data from Parse
    let query = PFQuery(className:"Party")
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error: NSError?) -> Void in

    if error == nil {
    if let objects = objects as? [PFObject] {
    for (index, object) in enumerate(objects) {
        // Convert PFObject into Party object
        let party = Party(pfObject: object)
        self.parties.append(party)
        }
    }

} else {
    // Log details of the failure 
    println("Error: \(error) \(error!.userInfo)")
    }
        dispatch_async(dispatch_get_main_queue()){
            //reload the table view
            self.discoverTableView.reloadData()
            query.cachePolicy = PFCachePolicy.NetworkElseCache
        }

}
    //query.cachePolicy = PFCachePolicy.NetworkElseCache
}

它神奇地工作了!希望这个解决方案对遇到这个问题的任何人都有用:)