1

So what I'm trying to do is retrieve information from a Parse.com database based on an attribute other than objectID. More specifically, I'd like to retrieve an object without using the objectID so I can read its ADDRESS column. The logic so far:

  • Write a query that returns the row where the restaurant name is the same as 'resto'.

  • (this is where i'm stuck) retrieve the restaurant's address from the PFQuery and assign that String value to the addressLabel.text variable. Here is the code I have:

    class FinalView: UIViewController{
    
    
    
        var resto = 'Eddies Cafe'
        var address:String!
        //var phone:String!
    
       @IBOutlet var restoLabel: UILabel! 
    
       @IBOutlet var addressLabel: UILabel!
    
       @IBOutlet var phoneLabel: UILabel!
    
    
       override func viewDidLoad() {
           super.viewDidLoad()
           //the below query returns only one row
           var query = PFQuery(className: "Restaurants")
           query.whereKey("NAME", equalTo: resto)
    
    
           var obj = PFObject.getObjectWithID("i dont want this")
    
           address = obj["ADDRESS"] as? String
           restoLabel.text = resto
           addressLabel.text = address
    
      }
    
    }
    

I've been reading plenty on how to do it, but all i'm seeing is how to retrieve a PFObject from a query by it's objectID. I know the objectId's myself since it's my parse.com instance, but I need the app to be able to retrieve the data values from the backend based on restaurant name. Is there a way around this? Am I going about it all wrong? Thanks in advance.

1 Answer 1

1

Try it with findObjectsInBackgroundWithBlock

var query = PFQuery(className:"Restaurant")
    query.whereKey("adress", equalTo:"Eddies Cafe")
    query.limit = 1
    query.findObjectsInBackgroundWithBlock {
        (restaurants: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            if let objects = restaurants as? [PFObject] {
                var firstObject = objects[0]
                dispatch_async(dispatch_get_main_queue(),{
                    self.restoLabel.text = firstObject["restaurant"] as? String
                    self.adressLabel.text = firstObject["adress"] as? String
                    self.phoneLabel.text =  firstObject["phone"] as? String
                })

            }
        }
    }

I used dispatch_async to process view updates in the main queue so you can see your result immediately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.