TommyF
TommyF

Reputation: 343

Parse.com findObjects() get data

I need to run a SYNCHRONOUS call to parse.com. This is what I got:

var query = PFQuery(className:"myClass")
    query.whereKey("groupClassId", equalTo:self.currentGroupId)
    query.selectKeys(["objectId", "firstName", "lastName"])
    self.arrayCurrentData = query.findObjects() as Array<myData>

This return the correct number of rows from parse.com and fills up my local array. But how can I extract the data from the array? If I look at the array at runtime it shows that all the data I need is in 'serverData' in self.arrayCurrentData.

Normally if I loop an async(findObjectsInBackgroundWithBlock) filled array I would ask

self.arrayCurrentData[i].lastName

to get the lastName, but that is not the case in the sync array. There I can't ask directly for values (or so it seems).

Anyone who know what I am talking about and how to get data synchronous from parse.com?

Upvotes: 0

Views: 1697

Answers (1)

danh
danh

Reputation: 62686

Get the PFObject's attributes with valueForKey(). This is true whether or not the object was fetched synchronously. In other words...

self.arrayCurrentData[i].valueForKey("lastName")

EDIT - This approach generates a compiler message because you've typed the response as Array<myData>. But find returns PFObjects, so ...

self.arrayCurrentData = query.findObjects() as [PFObject]

... is the correct cast. I'm not a swift speaker, but the expression self.arrayCurrentData[i].lastName pleases the compiler because arrayCurrentData[i] is typed as myData. But this fails at run time because the real returned objects are PFObjects.

As an aside, I'd take a hard look at the rationale for fetching synchronously. I can't think of a case where its a good idea on the main thread. (off the main okay, but then you've already opted for asynch vs. the main, and the block-based methods provide a good way to encapsulate the post-fetch logic).

Upvotes: 1

Related Questions