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.
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.
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)
}
- 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?
}