Jason
Jason

Reputation: 1067

UITableView in Parse, not indexing properly

All, I have a simple class in the parse backend - which feeds a uitableview. The data is stored in Arrays. I guess because all parse data is done in the background sometimes data gets downloaded before others. I have a very mixed up tableview. With images in the wrong cell etc. Also the custom cells are sometimes not showing up at all and I need to do a refresh.

Here is my code that I used to download all the data from parse and add into the arrays.

Can you have a look please and suggest a way to do this.

Also how can I add a placeholder image , before the original image comes up. Also this code is in my ViewDidLoad, would it be better in an earlier function, hopefully so i don't have to relsoaddata on the tableview..

 var query = PFQuery(className:"TableViewData")
        query.includeKey("EventLoc")
        query.findObjectsInBackgroundWithBlock{
            (objects: [AnyObject]!, error: NSError!) -> Void in

            if error == nil {

                for object in objects {

                    let thumbNail = object["backgroundImage"] as PFFile

                  thumbNail.getDataInBackgroundWithBlock({
                        (imageData: NSData!, error: NSError!) -> Void in
                        if (error == nil) {
                                dispatch_async(dispatch_get_main_queue()) {
                            let image = UIImage(data:imageData)
                            self.CellBackgroundImage.append(image!)
                            }
                        }
                        })



                    var VenueLocation = object["EventLoc"] as PFObject!
                    VenueLocation.fetchIfNeededInBackgroundWithBlock {
                        (VenueLocation: PFObject!, error: NSError!) -> Void in
                        dispatch_async(dispatch_get_main_queue()) {
                        let VenueLocationTitle = VenueLocation["EventLocation"] as NSString
                        self.EventLocationArray.append(VenueLocationTitle)
                        }

                    }


                    let eventiconimage = object["EventIcon"] as PFFile
                    eventiconimage.getDataInBackgroundWithBlock({
                        (imageData: NSData!, error: NSError!) -> Void in
                        if (error == nil) {
                            dispatch_async(dispatch_get_main_queue()) {
                            let image = UIImage(data:imageData)
                            self.EventIconImageArray.append(image!)
                            }

                        }
                    })

                    dispatch_async(dispatch_get_main_queue()) {
                    self.TitleArray.append(object["EventTitle"] as String)
                    self.EventPriceArray.append(object["EventPrice"] as String)
                    self.EventStartDate.append(object["EventStartDate"] as NSDate)
                    self.EventEndDate.append(object["EventEndDate"] as NSDate)
                    self.tableView.reloadData()
                    }

Upvotes: 1

Views: 193

Answers (1)

Shashi3456643
Shashi3456643

Reputation: 2041

You need to use serial queue then your array data will be in order. Whats happening because of concurrent task the data is not appended in order

var backgroundQueue:dispatch_queue_t =  dispatch_queue_create("SerialQueue", DISPATCH_QUEUE_SERIAL)

        var query = PFQuery(className:"TableViewData")
        query.includeKey("EventLoc")
        query.findObjectsInBackgroundWithBlock{
            (objects: [AnyObject]!, error: NSError!) -> Void in

            if error == nil {

                for object in objects {
             dispatch_async(backgroundQueue, { () -> () in


                    let thumbNail = object["backgroundImage"] as PFFile

                    thumbNail.getDataInBackgroundWithBlock({
                        (imageData: NSData!, error: NSError!) -> Void in
                        if (error == nil) {
                            dispatch_async(dispatch_get_main_queue()) {
                                let image = UIImage(data:imageData)
                                self.CellBackgroundImage.append(image!)
                            }
                        }
                    })



                    var VenueLocation = object["EventLoc"] as PFObject!
                    VenueLocation.fetchIfNeededInBackgroundWithBlock {
                        (VenueLocation: PFObject!, error: NSError!) -> Void in
                        dispatch_async(dispatch_get_main_queue()) {
                            let VenueLocationTitle = VenueLocation["EventLocation"] as NSString
                            self.EventLocationArray.append(VenueLocationTitle)
                        }

                    }


                    let eventiconimage = object["EventIcon"] as PFFile
                    eventiconimage.getDataInBackgroundWithBlock({
                        (imageData: NSData!, error: NSError!) -> Void in
                        if (error == nil) {
                            dispatch_async(dispatch_get_main_queue()) {
                                let image = UIImage(data:imageData)
                                self.EventIconImageArray.append(image!)
                            }

                        }
                    })

                    dispatch_async(dispatch_get_main_queue()) {
                        self.TitleArray.append(object["EventTitle"] as String)
                        self.EventPriceArray.append(object["EventPrice"] as String)
                        self.EventStartDate.append(object["EventStartDate"] as NSDate)
                        self.EventEndDate.append(object["EventEndDate"] as NSDate)
                        self.tableView.reloadData()
                    }

                });

                }
            }

Upvotes: 2

Related Questions