Retrieving, storing and accessing an array of user data at Parse.com

I am 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 do array in parsing, but I'm not sure if it works, it still says “undefined” in parsing when it should be an array of Food objects. My question is very simple.

1 - How to create an array of Food objects in Parse.com?

2 - How to get information from this array and parse a local variable?

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

4 - How, as soon as I have edited the array, will I save it for parsing?

: Only these four questions, I was told a lot of answers, such as “just use the query” or “create new objects”, but I gave the names of the actual objects, because I would appreciate it if you gave the code, see works, and then understand how it works; Thanks in advance for everything that will help you.

+4
source share
1 answer

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¥" //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) 

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.

  1. 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) } 
  1. 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? } 
+3
source

All Articles