Reputation: 18
I'm using Parse.com, and to get my wanted information, I use it's Queries.
So I use that one:
ParseUser PhotoeUser;
ParseQuery<Photo> UserQuery = ParseQuery.getQuery(Photo.class);
UserQuery.whereEqualTo("objectId", PhotoID);
UserQuery.getFirstInBackground(new GetCallback<Photo>() {
@Override
public void done(Photo pPhoto, ParseException error) {
PhotoUser = pPhoto.getUser();
}
});
So what I want is simply use the PhotoUser
outside of that query. But When I write:
Log.i("Profile", "PhotoUser = " + PhotoUser.get("objectId"));
PhotoUser.get("objectId")
is null
.
Another example is this one:
public ParseUser getUser(){
ParseQuery<Photo> UserQuery = ParseQuery.getQuery(Photo.class);
UserQuery.whereEqualTo("objectId", PhotoID);
UserQuery.getFirstInBackground(new GetCallback<Photo>() {
@Override
public void done(Photo pPhoto, ParseException error) {
PhotoUser = pPhoto.getUser();
}
});
return PhotoUser;
}
Here, when I want to return the PhotoUser
, it is null
.
Please help me. If you need more information, just ask for it.
Upvotes: 0
Views: 308
Reputation: 1350
Your method
public ParseUser getUser(){
ParseQuery<Photo> UserQuery = ParseQuery.getQuery(Photo.class);
UserQuery.whereEqualTo("objectId", PhotoID);
UserQuery.getFirstInBackground(new GetCallback<Photo>() {
@Override
public void done(Photo pPhoto, ParseException error) {
PhotoUser = pPhoto.getUser();
}
});
return PhotoUser;
}
returns null
because is simply is when you are returning it. You are creating a background task that sets PhotoUser
when it finishes, but instantly after creating the task you return your current reference to PhotoUser
. The task finishes a little time later and sets your PhotoUser
. So either you don't return anything and call some method when task finishes or you use ParseQuery.get()
which blocks until it has found the object.
Also, please name you member variables with a small letter at the start. (userQuery
and photoUser
).
Edit: As requested, change your code to:
public ParseUser getUser(){
ParseQuery<Photo> UserQuery = ParseQuery.getQuery(Photo.class);
UserQuery.whereEqualTo("objectId", PhotoID);
PhotoUser = UserQuery.getFirst().getUser();
return PhotoUser;
}
or
public void getUser(){
ParseQuery<Photo> UserQuery = ParseQuery.getQuery(Photo.class);
UserQuery.whereEqualTo("objectId", PhotoID);
UserQuery.getFirstInBackground(new GetCallback<Photo>() {
@Override
public void done(Photo pPhoto, ParseException error) {
PhotoUser = pPhoto.getUser();
doWork();
}
});
}
private void doWork() {
// do what you want to do with your PhotoUser, it is not null now
}
Upvotes: 1
Reputation: 4343
Just take ParseUser outside the block:
PhotoUser pUser;
ParseQuery<Photo> UserQuery = ParseQuery.getQuery(Photo.class);
UserQuery.whereEqualTo("objectId", PhotoID);
UserQuery.getFirstInBackground(new GetCallback<Photo>() {
@Override
public void done(Photo pPhoto, ParseException error) {
pUser = pPhoto.getUser();
}
});
Don't forget to give variables names. I don't know how do You want to get PhotoUser
when You query for Photo
.
Upvotes: 0