Search code examples
iosswiftparse-platformnullpfquery

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.

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!


Solution

  • Oh, I just solved the problem! All I did was change the getParties() function to:

    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 :)