简体   繁体   English

解析PFQuery返回nil(swift)

[英]Parse PFQuery returns nil (swift)

I am trying to add to an array of a class I created called "Party" called "parties" to use in a UITableView, but the function I created returns a nil value for the arrays even though there is one in Parse. 我试图添加到我创建的名为“ Party”的类的数组中,以在UITableView中使用,但是即使在Parse中有一个值,我创建的函数也会为该数组返回nil值。

I created the array in my view controller like this: 我在视图控制器中创建数组,如下所示:

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

And I've also tried this with the same result: 而且我也尝试了相同的结果:

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

The function looks like this: 该函数如下所示:

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()
        }
    }
}

I've also tried this: 我也尝试过这个:

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
}

regardless of which function I use, the array comes out empty. 不管我使用哪个函数,数组都为空。 Great thanks to anyone who can figure this out! 非常感谢任何可以弄清楚这一点的人!

Oh, I just solved the problem! 哦,我刚解决了这个问题! All I did was change the getParties() function to: 我所做的就是将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
}

and it magically worked! 它神奇地起作用了! Hopefully this solution will be useful to anyone else who runs into this problem :) 希望此解决方案对遇到此问题的其他人有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM