I have 2 pointer objects(pointing to ParseUser) in my table "Attack".It seems these pointer objects take a while to get retrieved.Hence my code directly didnt work and gave me the exception:
java.lang.IllegalStateException: ParseObject has no data for this key. Call fetchIfNeeded() to get the data.
I then did the needful surrounding the fetchIfNeeded function with a try-catch block:
ParseObject battle = null;
try {
battle = objects.get(0).fetchIfNeeded();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ParseUser attacker1 = battle.getParseUser("Attacker");
Log.i("dontest",attacker1.getUsername());
It is still returning the same.I even checked with isDataAvailable function and it returned true.Any way around this?
P.S.: My query returns exactly 1 row which I checked with the size() function.
Here's the documentation describing fetchIfNeeded() function.
Use Your query like this..
ParseQuery<ParseObject> pQueryFromAttack = new ParseQuery<ParseObject>("Attack");
// use include to fetch details while quering----increase loading speed by doing this.
pQueryFromAttack.include("pointer to _user table");
List<ParseObject> listObj = pQueryFromAttack.find();
for eg:
String userName =listObj.get(0).fetchIfNeeded().getString("name");
Use it like this...
Related
I want to pass a few dates to this method, and get a few objects from the DB in return.
QueryBuilder<WorkDayDB, Long> queryBuilder =
application.ormLiteDatabaseHelper.getWorkDayDBDao().queryBuilder();
Where where = queryBuilder.where();
try {
where.eq("date", dates);
return queryBuilder.prepare();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
However, the where.eq("date", dates); throws an exception, saying it needs a single Date, not an array of dates.
Apparently eq means equals which is specifically used for a single argument, whereas in (which is another method of where in OrmLite) allows for an array of arguments to be passed. This solved my problem.
Hi i try to retrieve data from 2 different tables in android using parse but with no success. I want to retrieve "titolo" from "Luoghi" table for all my "imageFile" in Photos table. This is my
databse and this is my code
list = new ArrayList<>();
try {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Photos");
query.include("luogoPointer");
listaEventiParse = query.find();
for (ParseObject country : listaEventiParse) {
// Locate images in flag column
ParseFile image = (ParseFile) country.get("imageFile");
ParseObject luoghi= country.getParseObject("luogoPointer");
ListaEventiItem map = new ListaEventiItem();
map.setImmagine(image.getUrl());
map.setId(luoghi.getString("titolo"));
list.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
i got java.lang.NullPointerException on luoghi.getString("titolo").
what i am missing?
You included luogoPointer in your query but many entries in your Photos table for this pointer is blank. When you get Photos result then you got null pointer for many records and calling getString() on this null pointer, that's why you are getting NullPointerException.
Include one more condition in your query to select only that record which contain luogoPointer.
query.whereExists("luogoPointer");
Now your query is like this :
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Photos");
query.include("luogoPointer");
query.whereExists("luogoPointer");
listaEventiParse = query.find();
Instead of including only the pointer to include the field (or fields) you want to retrieve before the .find() call:
query.include("luogoPointer.titolo");
I'm trying to get a query using as condition the Pointer Id of the object, for example I'm saving all the id's of an object in an array, then I want to get from another class (related by pointer), all the objects that uses that Id, so I already have this:
for (i = 0; i < num; i++) {
//restaurant.setObjectId(restId[i]);
ParseQuery<ParseObject> resultsitems = ParseQuery.getQuery("Item").whereEqualTo ("restaurant", restId[i]);
try {
objects=resultsitems.find();
} catch (ParseException e) {
e.printStackTrace();
}
(.......)
}
In my first try I tried to set the id into the restaurant object, then tried to use the query as:
ParseQuery<ParseObject> resultsitems = ParseQuery.getQuery("Item").whereEqualTo ("restaurant",restaurant );
But it didn't work, then I tried to search as shown in the code above, it doesn't crash but brings me nothing, how can I do this?
This is what really worked for me:
ParseObject obj = ParseObject.creatWithoutData("classNameThatPointedTo","fieldValue");
query.whereEqualTo("fieldName", obj);
Use: .findInBackground(new FindCallBack<Item>... (this will auto-complete in Android Studio), then put objects=resultsitems.find(); in the curly braces of the done() function.
The callback waits for the query to return before moving on with the script. Otherwise, the main thread will keep moving on without waiting for the data to come back from the server.
I have made a table in Parse.com using Dashboard name Blogs. I have made a column url type String using Dashboard inside it where I have to post blogs url and I have posted some url inside this column by adding rows. Now I am trying to get these values inside my application by using this code in onCreate() method.
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Blogs");
parseQuery.whereEqualTo("url", true);
List<ParseObject> objects = null;
try {
objects = parseQuery.find();
}
catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
I am getting objects of size 0. I am unable to understand how to get my url column all values.
Read the api docs carefully.
I believe you want to query if the urls exists in the column. (since you have used parseQuery.whereEqualTo("url", true);)
From the docs,
public ParseQuery<T> whereEqualTo(String key,
Object value)
What it does : Add a constraint to the query that requires a particular key's value to be equal to the provided value.
So in this case for url, the value should be a String (which you want to match to) if you have defined the column as String. Putting value as boolean will give you undesired result.
If you want to check if the column url exists for the object,
then use whereExists(String key).
Use like this
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Blogs");
parseQuery.whereEqualTo("url","true");
List<ParseObject> objects = null;
try
{
objects = parseQuery.find();
}
catch (ParseException e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
Hi I was wondering how I can get a ParseUser object from a ParseObject. I need to do this because a ParseQuery returns a List. Here is my code and thank you for the help!
// get the currentUser
currentUser = ParseUser.getCurrentUser();
List<ParseObject> friendsList = currentUser.getList("friendsList");
// search for the person the user wants to add as a friend
List<ParseObject> userResults = new ArrayList<ParseObject>();
ParseQuery otherUserQuery = ParseUser.getQuery();
otherUserQuery.whereEqualTo("username", "jyo2");
try {
userResults = otherUserQuery.find();
} catch (ParseException e1) {
// fail
e1.printStackTrace();
}
// get the friend from the query if there was one and add it to the
// currentUser friends list
if (userResults.size() != 0) {
ParseObject currentObject = userResults.get(0);
friendsList.add(currentObject);
currentUser.put("friendsList", friendsList);
}
// save the update
try {
currentUser.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// try to view the friends
List<ParseObject> currentFL = currentUser.getList("friendsList");
ParseObject currentPU = currentFL.get(0);
System.out.println("THIS IS SIZE" + currentFL.size());
System.out.println(currentPU.get("name"));
Use the existing "User" class in Parse but subclass it using
#ParseClassName("_User")
public class PUser extends ParseUser {
As per this article. You actually specifically need to refer to the _User "ParseClassName" when subclassing this object. It's super hairy, I was stuck on this for ages because for other classes you only need to "register" the class with Parse using it's literal name as per the Parse Data Browser, which in this case is "User", "Info", "Post" etc, but the User class specifically requires the underscore
Edit Sorry, I should have mentioned, then you simply cast the subclass PUser when you get your returned objects from the query. You may need to change the query to be a User query instead of an object one too.
I know this is an old question, but this wasn't clear for me and there wasn't a complete thread on the issue I had, on SO. Hope this helped.
I don't think you need to "cast" is to ParseUser, a parseuser is a parseobject as well, apart from having some basic pre-defined properties attached to it, which you can anyways query like you'd query any other parseobject