4

I'm working on some code where I have a Food object and I have an array of Food objects: var list: [Food] = [] (this line of code is called right at the beginning as a global variable) I have lines of code that make an array in parse but i'm not really sure it is working, it still says "undefined" in parse when it should be a Food Object array. My question is really simple.

1 - How do I make a Food object array in Parse.com?

2 - How do I get the information from that array and make the parse array a local variable?

3 - How to append that array, I know I can just call .append(customFoodObject) to the local array, is that how I should do it?

4 - How, once i've edited the array, do I save it back to parse?

: Just those 4 questions, i've been told many answers like "just use a query" or "make new objects" but I gave the names of the actual objects because I would appreciate it if you gave code that I could see works and then understand how it works; thanks in advance for all you help.

1 Answer 1

3

Anyway the questions demands for a huge manuscript, nonetheless i will try to answer your question with some explanations and code snippets where possible.

Objects contain similar stuffs so they would be a row in a table in a database. After all Parse is a sugar topped database API. So a object would be mapped to a Class in Parse or specifically a row of the class of type Food.

  1. Creating a Food object in Parse is pretty straightforward as the documentation is fairly explanatory.

    let food = PFObject(className: "Food")
    food["name"] = "Sushi Pizza"
    food["price"] = "1100¥"
    
    //saves on the server even if the networks fails now
    //caches the result offline and when the phone enters in network it uploads
    food.saveEventually(nil)
    

For storing array of foods do this:

    let foodClassName = "Food"
    for index in foods!{
        let object          = PFObject(className: foodClassName)
        object["name"]      = index.name
        object["price"]     = index.price
        object.saveEventually(nil)
    }

Basically you create a Food table with the className and then insert similar objects and save it.

  1. Getting array is querying the parse database. All you have to know is the name of the Class Parse uses. In our case we had if "Food" stored in a constant.

    let query = PFQuery(className: foodClassName)
    //query.fromLocalDatastore()    //uncomment to query from the offline store
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil && objects != nil{
            for index in objects!{
                (index as! PFObject).pin()      //pining saves the object in offline parse db
                //the offline parse db is created automatically
                self.makeLocalVariable(index as! PFObject)  //this makes the local food object
            }
        }
    }
    

So to save it to locl Food object we initially had we transform the PFObject like so

var localFoods:[Food]?          //global scoped variable
func makeLocalVariable(index:PFObject){
    let foodname = index.objectForKey("name") as! String
    let price = index.objectForKey("price") as! String          //we had the currrecny saved too

    let foodObj = Food(name: foodname, price: price)
    localFoods?.append(foodObj)
}
  1. Yes i did it that way. Thats correct.

4.So the process is basically similar to fetching data. Now what you do is suppose fetch data for Food with name Pizza because we want to increase the price. Heres how you do that.

    let queryEdit = PFQuery(className: foodClassName)
    queryEdit.whereKey("name", equalTo: "Pizza")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil && objects != nil{
            if objects!.count == 0{
                //edit
                let obj = (objects as! [PFObject]).first!
                obj.setValue("2340¥", forKey: "price")
                //save it back
                obj.saveEventually(nil)
            }
        }
    }

I hoped i answered your questions. Remember a Object can be mapped to Class in Parse or Table or Entity in a Relation Database. Then that table can have many similar instances and you can say they are array of one type.

Let me know if i can help you more. Cheers!

Food for this example is just a Struct but can be easily a class if it has functionalities.

struct Food{
   var name:String?
   var price:String?
}
Sign up to request clarification or add additional context in comments.

5 Comments

huh, i made a sepperate class for the object, and now i would use a query to get the information from parse?
also, i want the object food array to be user specific, i already have a user class, can i save an object to a user? or do i have to make a different class?
having food object user specific can be achieved by having a id which references in the food table in the database. Think in terms of database relationship. userid | username | password Now for food you would have something like this foodid | userid | name | price. the userid gives you a reference to the user with whom the food is attached.
dude do you still get a notification when i say this i have one small question on the information given
if you do see it. i was wondering how i would change or edit a pfobject and then send it back but not make a duplicate with a slight change

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.