0

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?

1
  • Using self.arrayCurrentData[i].lastName should work in this case. Are you getting an error? Provide more information please. Commented Mar 3, 2015 at 17:54

1 Answer 1

1

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

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

2 Comments

Thanks for the answer, but I have already tried that. The error when I try self.arrayCurrentData[i].valueForKey("lastName") is: 'Could not find member valueForKey'.
And his name was BINGO! Thanks very much for your help danh!

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.