Reputation: 204746
In my Activity I init the Parse service:
Parse.enableLocalDatastore(getApplicationContext());
ParseObject.registerSubclass(Person.class);
ParseObject.registerSubclass(Area.class);
Parse.initialize(this, "MY_APP_ID", "MY_CLIENT_KEY");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
I have this class
@ParseClassName("Area")
public class Area extends ParseObject {
public static ParseQuery<Area> getQuery() {
return ParseQuery.getQuery(Area.class);
}
public String getObjectId() {
return getString("objectId");
}
public String getName() {
return getString("name");
}
public void setName(String name) {
put("name", name);
}
}
First I want to retrieve data from the Parse service like this
ParseQuery<Area> areaQuery = Area.getQuery();
areaQuery.findInBackground(new FindCallback<Area>() {
public void done(List<Area> areas, ParseException e) {
if (e == null) {
ParseObject.pinAllInBackground(areas,
new SaveCallback() {
public void done(ParseException e) {
//output errors
}
});
} else {
//output errors
}
}
});
but the ParseObjects do not have an objectId
.
Later I want to retrieve data from my local datastore like this
ParseQuery<Area> query = Area.getQuery();
query.orderByAscending("name");
query.fromLocalDatastore();
query.findInBackground(new FindCallback<Area>() {
public void done(List<Area> areaList, ParseException e) {
if (e != null) {
Log.d("area", "Error retrieving areas");
return;
}
for (Area a : areaList)
areaResults.add(a);
}
});
That always throws a ParseException
:
com.parse.ParseException: java.lang.IllegalStateException: ParseObject has no data for 'objectId'. Call fetchIfNeeded() to get the data.
Well because the ParseObjects actually do not have an objectId
. Neither in my local datastore nor when querying directly from Parse.
What is going wrong here? I took the code from various examples and it should work.
Upvotes: 0
Views: 1009
Reputation: 9932
Update
You have included a getter for objectId in your subclass. This is already accessible from the parent class and should not overridden.
Old answer
The problem is probably that objects in local datastore does not have an objectId until it has been saved remotely to Parse. Only then does it get an objectId.
You need to make sure your objects have been saved remotely OR you need to pin objects locally before querying them. In the case of pinning, you cannot access the objectId unless it has also been saved remotely.
Upvotes: 2
Reputation: 1721
The problem is getString("objectId");
. You must not implement default Parse Column getters/setters
.
You can get it with getObjectId()
which has already been implemented in ParseObject. Also to get updatedAt
, createdAt
and some other default Parse column, you have to use their own Getters
such as getUpdatedAt()
and getCreatedAt()
.
At the below, I removed your objectId
getter implementation. You should implement only your own column fields.
@ParseClassName("Area")
public class Area extends ParseObject {
public static ParseQuery<Area> getQuery() {
return ParseQuery.getQuery(Area.class);
}
public String getName() {
return getString("name");
}
public void setName(String name) {
put("name", name);
}
}
Upvotes: 3