简体   繁体   中英

Parse.com: subclassing ParseObject and calling getParseObject

I'm using the parse.com Android SDK. I have a table Foo with a pointer to the table Bar, and classes Foo and Bar that extend ParseObject. In Foo, I have this method:

public void getBar(final GetCallback<Bar> callback){
    getParseObject("bar").fetchIfNeededInBackground(callback);
}

This is the error I get:

java.lang.ClassCastException: com.parse.ParseObject cannot be cast to com.package.Bar

getParseObject returns a ParseObject, but fetchIfNeededInBackground won't return the subclass, just ParseObject. The documentation describes it so:

public final <T extends ParseObject> void fetchIfNeededInBackground(GetCallback<T> callback)

To me, that seems to indicate that it will give back a subclass of ParseObject according to the type of callback object you pass in. On the other hand, fetch says:

public <T extends ParseObject> T fetch()

And I don't see how you can supply a subtype to that method. Do I need to call a fetch method on an instance of Bar in order for this to work? If so, how do I get a Bar from my Foo ? I think I could call getParseObject , create a new Bar , and copy the values from one to the other, but that's really gross and I'm hoping I don't have to shower every time I see this code.

I played around with doing it a different way and then parse gave me a more helpful error message: the Bar subclass wasn't registered. So if you can't figure out why you can't cast to your subclass, make sure it's registered, because the exception may not mention that as the cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM