lundi 27 juin 2016

GeoFire Query running twice, returning no results

I am querying GeoFire for a list of food trucks/foot truck info, then populating a UITableView with that list. However, when I run this in my simulator, the page crashes and says that the array index is out of range. It looks like I'm losing everything in my truckPhotos array when I exit that block.

One thing I've noticed is that print statements placed inside the query print twice, and I cannot figure out why.

My GeoFire Query:

    //Query GeoFire for nearby users
    //Set up query parameters
    let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
    let circleQuery = geoFire.queryAtLocation(center, withRadius: 10)
    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in
        if key == ref.authData.uid{
            print("Found myself! Omitted.")
        }else{
            self.truckKeys.append(key)
            print(self.truckKeys)
            //prints result twice
            self.truckLocations.append(location)
        }

    }) //End truckQuery


    //Execute code once GeoFire is done with its query
    circleQuery.observeReadyWithBlock({
        print(self.truckKeys)

        //Calculate the distances from the user, and store them in an array
        for location in self.truckLocations{
            let truckLocation = location
            let distance = self.currentLocation.distanceFromLocation(truckLocation)*0.000621371
            self.truckDistances.append(String(format: "%.1f", distance))
            let dropPin = MKPointAnnotation()
            dropPin.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
            self.mapAnnotations.append(dropPin)
            self.map.addAnnotation(dropPin)
            self.truckList.reloadData()
        }
        for key in self.truckKeys{
            //Grab the names of the trucks, and store them in another array
            ref.childByAppendingPath("users/(key)/name").observeEventType(.Value, withBlock: {
                snapshot in
                self.truckNames.append(snapshot.value as! String)
            })
            //Grab the profile images, and store them in another array
            ref.childByAppendingPath("users/(key)/profileImage").observeEventType(.Value, withBlock: { snapshot in
                if snapshot.value != nil{
                    let base64String = snapshot.value as! NSString
                    let decodedData = NSData(base64EncodedString: base64String as String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
                    let decodedImage = UIImage(data: decodedData!)

                    self.truckPhotos.append(decodedImage!)
                }else{
                    self.truckPhotos.append(UIImage(named: "Placeholder.jpg")!)
                }
            })
            print(self.truckPhotos.count)
            //Prints "0" 8 times, twice for each key in truckKey
            //Grab the descriptions of the trucks, and store them in another array
            ref.childByAppendingPath("users/(key)/selfDescription").observeEventType(.Value, withBlock: {
                snapshot in
                self.truckDescriptions.append(snapshot.value as! String)
                self.truckList.reloadData()
            })
        }
    }) //End observeReadyWithBlock

My CellForRowAtIndexPath

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.truckList.dequeueReusableCellWithIdentifier("competitorsCell", forIndexPath: indexPath) as! competitorsCell

    cell.separatorInset = UIEdgeInsetsZero

    //Set all the info from our arrays
    cell.truckPhoto.layer.cornerRadius = cell.truckPhoto.frame.size.width / 2
    cell.truckPhoto.clipsToBounds = true
    cell.truckPhoto.layer.borderWidth = 0.0
    let borderGrey = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3).CGColor
    cell.truckPhoto.layer.borderColor = borderGrey
    cell.truckPhoto.image = self.truckPhotos[indexPath.row]
    //Returns an Array Index Out Of Range error

    cell.truckName.text = self.truckNames[indexPath.row]
    cell.truckDescription.text = self.truckDescriptions[indexPath.row]
    cell.truckDistance.text = "(self.truckDistances[indexPath.row]) mi"


    return cell

}

Thanks for any help!

Aucun commentaire:

Enregistrer un commentaire