1

I am building and app that saves an object in the local datastore with parse. I then run a query to retrieve the objects that are in the local datastore and it is working fine. however, I would like to grab the object, and the contents in it, and set some labels in a table view cell based on the items that are stored in the parse local data store object. for example, i make an object with attributes like "objectID", "name", "date", "location". what i'd like to do is to have a table view on the home screen that displays the name, date, location ...etc. of each item that was saved in local datastore in labels in each cell.

i know that im saving it correctly:

// parse location object

    let parseLighthouse = PFObject(className: "ParseLighthouse")
    parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")
            parseLighthouse["Name"] = self.placeTitle.text
            parseLighthouse["Note"] = self.placeNote.text
            parseLighthouse["Locality"] = self.placeDisplay.text!
            parseLighthouse["Latt"] = self.map.region.center.latitude
            parseLighthouse["Longi"] = self.map.region.center.longitude
            parseLighthouse["LattDelta"] = 0.5
            parseLighthouse["LongiDelta"] = 0.5
            parseLighthouse["Date"] = dateInFormat
            parseLighthouse.pinInBackground()
            parseLighthouse.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
                println("Object has been saved. ID = \(parseLighthouse.objectId)")
            }

and when i run the query, im able to access the attributes by running println(object.objectForKey("Name"))

func performQuery() {
    let query = PFQuery(className: "ParseLighthouse")

    query.fromLocalDatastore()
    query.whereKey("User", equalTo: PFUser.currentUser()!)
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) lighthouses.")
            // Do something with the found objects
            if let light = objects as? [PFObject] {
                for object in light {
                    println(object.objectId)
                    println(object.objectForKey("Name"))



                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }

because when running the query, i get back the object id and name as expected.

Successfully retrieved 2 lighthouses. Optional("A3OROVAMIj") Optional(happy) Optional("bbyqPZDg8W") Optional(date test)

what I would like to do is grab the name field within the parse object local data store, and that be the name of the label on a cell in a table view controller.

i dont know how to access that info from the object, and set the label correctly.

does anyone know how this is possible?

5
  • Do you have your tableView ready? So you can start accessing the data from parse and displaying it there? or you are asking how to do the tableView with parse.com data? Commented Sep 6, 2015 at 23:01
  • Are you saying that you are getting an object back that is called "object" but it is in fact a custom class object that you are working with? Have you tried casting it to the actual class and accessing the property that way? In Objective C you would do something like this to access the name property: ((PFUser*)object).name although I'm not sure what the swift equivalent is. Commented Sep 6, 2015 at 23:03
  • so you query function is working right, you should want to get those information then load all cells in your tableview... Right?? Commented Sep 6, 2015 at 23:09
  • yeah i currently have a table view setup. I guess my main issue is having trouble accessing the properties in the parse object.when I make one it saves with the customer class "ParseLighthouse" and in that object there is a name, latitude longitude note date...etc. I want to be able to grab those names notes...etc from ParseLighthouse and set that as labels in my travel view Commented Sep 6, 2015 at 23:10
  • just give me a second, i am writing the answer Commented Sep 6, 2015 at 23:13

1 Answer 1

1

It's always a good idea to avoid pointer lol ... so why not saving the userid or username with the specific object.. so change this line:

 parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")

TO

 parseLighthouse["username"] = PFUser.currentUser().username

Answer

NOW let's create a struct that contains the objectID and the Name outside of your Controller Class.

struct Data
{
var Name:String!
var id:String!
}

then inside of the Controller class, declare the following line of code globally

 var ArrayToPopulateCells = [Data]()

Then your query function will look like :

 func performQuery() {
    let query = PFQuery(className: "ParseLighthouse")

    query.fromLocalDatastore()
    query.whereKey("User", equalTo: PFUser.currentUser()!)
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            // The find succeeded.
            print("Successfully retrieved \(objects!.count) lighthouses.")
            // Do something with the found objects
            if let light = objects as? [PFObject] {
                for object in light {
                    print(object.objectId)
                    print(object.objectForKey("Name"))
                    var singleData = Data()
                    singleData.id = object.objectId
                    singleData.Name = object["Name"] as! String

                    self.ArrayToPopulateCells.append(singleData)


                }
            }
        } else {
            // Log details of the failure
            print("Error: \(error!) \(error!.userInfo)")
        }
    }

In the tableView numberOfRowinSection()

return ArrayToPopulateCells.count

In the cellForRowAtIndexPath()

       var data = ArrayToPopulateCells[indexPath.row]
       cell.textlabel.text = data.objectID
       cell.detailLabel.text = data.Name

VOila that should be it

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

8 Comments

ok i followed your instructions and they seemed to be doing alright, no errors. but nothing is showing up in the table view. i followed the code exactly and theres nothing there. im not entirely sure that the items are being added to the arrayToPopulateCells array. any ideas?
Did you reload the tableview
yes and it's working now. thanks. another question, do you know how I could now swipe to delete an item in the cell from local data store and from parse.com?
I think that method is called commiteditingstyle() once enables it choose the delete style then you could swipe to delete
any idea on how to target the parse object within the commitededitingstyle method? I'm able to swipe and delete the cell but want to target the parse object and unpin it and delete it. the parse docs help but I can't seem to get it right
|

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.