Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Parse query returns PFObjects, but when accessing the items become nil

I'm currently querying a parse database for a set of objects and then accessing the PFFile to display it as an image in a collectionview.

The parse query is here and it does return the objects I want. When I iterate through the objects in the query part of the code and display it, there doesn't seem to be a problem in showing the data.

    var topFitnessLocations: NSArray = []
    override func viewDidLoad() {
        super.viewDidLoad()

        //queries the parse for the top 3 locations to show on the home screen
        let predicate = NSPredicate(format:"Rank <= 3")
        var query = PFQuery(className:"fitnessLocations", predicate:predicate)
        //query Parse
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {
                // The find succeeded.
                self.topFitnessLocations = objects
                self.collectionView?.reloadData()
                println(self.topFitnessLocations.objectAtIndex(0)["bannerPicture"])
            } else {
                // Log details of the failure
                NSLog("Error: %@ %@", error, error.userInfo!)
            }
        }

Later on in the code I set the number of cells (which i refresh after my query is done in the background), but when I go into the function where the cell is given its data from the parse query, the items are nil. I tried println out the items I'm assigning to the cell and they are all nil.

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (topFitnessLocations.count)
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell: mainCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as mainCollectionViewCell
        // Configure the cell
        cell.backgroundColor = UIColor.grayColor()
        cell.imageView?.image = topFitnessLocations.objectAtIndex(indexPath.item)["bannerPicure"] as? UIImage
        println(indexPath.item)
        println(topFitnessLocations.objectAtIndex(indexPath.item)["bannerPicure"])
        println(topFitnessLocations.objectAtIndex(indexPath.item)["objectID"])
        return cell
    }

Just wanted to get some help on what I'm missing here. Thanks!

1 Answer

In your two override functions, should it be self.topFitnessLocations?