• Create Account

## "Parse" and Querying

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Ibzy  Members

257
Like
0Likes
Like

Posted 05 November 2013 - 11:24 AM

Hi All,

I have hit a bit of a snag on Android with regards to Querying a database.

I have set up an account with Parse.com which seems a very reasonable and clever solution to server hosting as a new developer.

In my set up I have 3 tables (or as Parse call them, classes) - Users, Cards, and Payer_Cards. All Users have a unique objectID, all Cards have a unique objectID and Player_Cards is a lookup between the two, containing Player ID and Card ID (not all players have all cards).

The below logic is my attempt, but for some reason I cant figure it out.  I can get the Card IDs listed based on all cards against the player ID (currentUser), but when I try to expand this to look at table 3 (Cards) it all falls apart.

public void cardList(List<ParseObject> cardList){
StringBuilder cards = new StringBuilder();
for(int i=0; i<cardList.size(); i++){
cards.append("Card:");

ParseObject[] cList = cardList.toArray(new ParseObject[0]);
String cardId = cList[i].getString("CARD_ID");

ParseQuery<ParseObject> query = ParseQuery.getQuery("CARDS");
query.whereEqualTo("objectId", cardId);
query.getInBackground(cardId, new GetCallback<ParseObject>() {
public void done(ParseObject object, com.parse.ParseException e) {
if (e == null) {
cards.append(object.getString("CARD_NAME"));
} else {

}
}
});

}

TextView CardsList = (TextView) findViewById(R.id.CardsList);
CardsList.setText(cards.toString());
}


The current error in my code is that "cards" isn't recognised within the if block.

Any assistance on this is greatly appreciated - I have exhausted all of my ides of how to get around this.

Thanks

Edited by Ibzy, 05 November 2013 - 11:25 AM.

### #2TheChubu  Members

8955
Like
0Likes
Like

Posted 05 November 2013 - 11:33 AM

I'm preety sure cards isn't visible within the if block because you're overriding a method with that anonymous class. That method can't see whats within cardList method (ie, the cards StringBuilder).

You're basically referencing a local variable that is inside another method that your GetCallback object simply doesn't knows about.

At most, from inner classes (or this anonymous class) you can reference fields of the enclosing object. ie, if cards were a field of whatever object has that cardList method, then you could access it from the anonymous class (and with a bit of magic from javac if the field isn't public).

Edited by TheChubu, 05 November 2013 - 11:36 AM.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

### #3swiftcoder  Senior Moderators

17832
Like
2Likes
Like

Posted 05 November 2013 - 11:41 AM

You should be able to make your code compile by declaring cards to be final. This will make it accessible within the anonymous class.

However, that won't actually make what you are trying to do work. It is highly unlikely that the GetCallback<ParseObject>.done() will be invoked until sometime after the cardList() method has already returned, which means that your textview will be set to an empty string.

You really need to make StringBuilder an instance variable, and update the textview in each parse callback.

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #4rip-off  Moderators

10730
Like
1Likes
Like

Posted 05 November 2013 - 11:41 AM

You can also reference local variables or parameters marked as final. Given that you don't appear to assign to "cards", this should work compile here.

Edit: as swiftcoder mentions, an asynchronous call like that cannot be guaranteed to have run by the time you try to use the variable. In addition to his suggestions, you may also have to deal with threading here, which means you'll need to correctly synchronise your code, or use a thread safe class (such as StringBuffer). Read the documentation very carefully about how getInBackground() works.

Edited by rip-off, 05 November 2013 - 11:45 AM.

### #5Ibzy  Members

257
Like
0Likes
Like

Posted 05 November 2013 - 11:43 AM

Hi Chubu,

Thanks - that makes a lot of sense, and is kind of what I thought was happening.  Can you think of a way to get around this?

I considered setting a public variable and having it set to the new string on each tick of the for loop, but seems to return null.

I imagine the main problem here is that Parse use a bit of their own terminology which no-Parse users wouldn't be familiar with?  If the query didn't need that public void done() I'd probably be ok.

Thanks

### #6Ibzy  Members

257
Like
0Likes
Like

Posted 05 November 2013 - 11:50 AM

Hi Guys,

Thanks for the pointer - setting the StringBuilder as an instance var works like a charm when it comes to letting me update it from various functions.

Now I just need to figure out why the CARD_NAME is blank

Thanks again!

### #7Ibzy  Members

257
Like
0Likes
Like

Posted 05 November 2013 - 12:03 PM

Hi Again,

After some debugging I found that it is not blank, the StringBuilder just isn't populated prior to being set.  Very much a simpleton question here I'm sure, but how do I synchronise my code? I've always thought it runs in order and each function is completed before it moves on.

I've changed StringBuilder to StringBuffer, and the app still compiles, but the data is still blank? I assume this is due to the synchronising you mention?

Thanks

Edit:

My ParseQuery public void section now looks like this:

public void done(ParseObject object, com.parse.ParseException e) {
if (e == null) {
cards.append("Card: ");
Log.d("Card","Contains " + object.getString("CARD_NAME"));
cards.append(object.getString("CARD_NAME")+"\n");

TextView CardsList = (TextView) findViewById(R.id.CardsList);
CardsList.setText(cards.toString());
} else {

}
}


This compiles and gives me the list I expect, however when the page loads I can see the list being populated - is this something I need to drag into the very beginning and cache, or am I doing something wrong which slows it all down?

Thanks

Edited by Ibzy, 05 November 2013 - 12:08 PM.

### #8rip-off  Moderators

10730
Like
0Likes
Like

Posted 05 November 2013 - 01:30 PM

You are kicking off a number of background jobs in a loop there. As they return, the list gets updated with the results. Perhaps the code could be modified to use one of the builk queries that the ParseQuery API appears to offer?

It is hard to offer any advice about what you should be doing without knowing the specifics of what your application is trying to achieve.

Edited by rip-off, 05 November 2013 - 01:32 PM.

### #9Ibzy  Members

257
Like
0Likes
Like

Posted 06 November 2013 - 03:42 AM

I have looked at the documentation around the ParseQuery API but am only seeing example queries for single table queries (If Parse supported SQL I would have this nailed).

Apologies for being rather vague in my question - I felt that general practice would cover the issues I'm having here, so maybe that's not the case.  My end goal is a card game (as I imagine you may have guessed), but with this snippet what I am trying to achieve is a list of cards owned by the current user.  There will be situations where cards get added, I would need a separate list for a deck which will contain some of the cards in the entire list, and perhaps the ability to trade cards with other players (this part is a long way off).

I know it might appear as though I'm starting from the middle, however I am trying to ensure I can fully query the Parse database in the way that I need to before deciding it is what I want.  Having read about peoples issues when taking a standalone app and making it work with users/logins, I am covering this part off first and adding the game logic to it.

Cheers

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.