In any case, the questions require a huge manuscript, nevertheless I will try to answer your question with some explanations and code snippets, where possible.
Objects contain similar materials, so they will be a row in a table in the database. In the end, Parse is a sugar coated database API. In this way, the object will be mapped to a class in Parse, or specifically to a series of class like Food.
Creating a Food object in Parse is quite simple, as the documentation is reasonably understandable.
let food = PFObject(className: "Food") food["name"] = "Sushi Pizza" food["price"] = "1100¥"
To store an array of products, do the following:
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 a class name, and then insert similar objects and save them.
Getting an array of queries to the parsing database. All you need to know is the class name Parse uses. In our case, we had if the "Food" is 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 for a local food facility, we initially decided to convert PFObject in this way
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) }
- Yes, I did it that way. Correctly.
4. Thus, the process is basically similar to data sampling. Now, what do you do, suppose you get data for Food named Pizza, because we want to increase the price. This is how you do it.
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 was hoping I answered your questions. Remember that an object can be mapped to a class in Parse or Table or Entity in a relationship database. Then this table can have many similar instances, and you can say that they are an array of the same type.
Let me know if I can help you more. Hurrah!
The food for this example is just Struct, but can be easily a class if it has functionality.
struct Food{ var name:String? var price:String? }