2

If we wanna get an object ID we should do this:

String objectId = gameScore.getObjectId();

but what if we wanna get an object ID by a query? Like this:

ParseQuery<ParseObject> query = ParseQuery.getQuery("mytable");
query.whereEqualTo("Title", "Adrians Book");

List<ParseObject> results = null;

 try {

        results = query.find();

        if(!results.isEmpty()) {
        String objectId = results.getObjectId();
        }

    } catch (com.parse4cn1.ParseException e) {

        Dialog.show("Err", "Something went wrong.", "OK", null);
    }

Sounds interesting don't you think? I wish it could be possible. As you can see in this example the query will get a value from a specific object in the table which could track for the object ID then returning it as well. ParseQuery class should be implemented with getObjectId(). Because by this way applications always could have access to object IDs from the query even after applications get restarted so in the first example the gameScore which is actually an instance of ParseObject would lost reference to the Database after restarting. Getting object IDs by the query it would be able to program applications to get object IDs automatically without the need of doing it manually nor depending on instances of ParseObject.

Simple
  • 827
  • 1
  • 9
  • 21
  • Is there a question here? Or are you just venting about the way you think the parse-server API *ought* to work? – paulsm4 Mar 30 '18 at 00:44
  • @paulsm4 Yep, there is. If you know How to get an object ID by query? Please let me know. – Simple Mar 30 '18 at 01:29

2 Answers2

1

@Shai Almog: Thank you very much for taking your time to look at the ParseQuery documentation. I accidentally figured out how to get this done!

ParseQuery<ParseObject> query = ParseQuery.getQuery("mytable");
query.whereEqualTo("Title", "Adrians Book");

List<ParseObject> results = null;

 try {
        results = query.find();
        if(!results.isEmpty()) {
        String objectId = results.get(0).getObjectId();
        System.out.println(objectId);
        }

    } catch (com.parse4cn1.ParseException e) {

        Dialog.show("Err", "Something went wrong.", "OK", null);
    }

Yep, after adding the method .get(index) it allows you to access the method .getObjectId() since results is a list of a ParseObject, then the respective objectId of your query result will be printed in the console! I'm pretty glad it's working because I won't need to serialize each object for now which would be a pain.

Also if you wanna set an instance of ParseObject with an existing objectId in case you need to update something in your Database, you can use this example:

ParseObject po = ParseObject.create("mytable");
po.setObjectId(//YOUR DESIRED OBJECTID HERE, AS LONG AS IT EXISTS IN THE DATABASE);
Simple
  • 827
  • 1
  • 9
  • 21
  • Parse automatically generates unique object IDs when you save objects. Why would you want to set the object ID manually? What's your use case? Here's the API doc for setObjectId(): "In general you do not need to use this. However, in some cases this can be convenient. For example, if you are serializing a ParseObject yourself and wish to recreate it [locally], you can use this to recreate the ParseObject exactly." – netsuite_insights Mar 31 '18 at 17:11
  • @sidiabale I'm not setting IDs manually anymore, now my application does it for me instead. If you have a method to whenever an application register a product, let's say a book for a Library then it creates objects automatically by the application serializing them as well. I'd be grateful. Since I don't have this method my application is getting IDs by query, it's working fine for me tho. And yeah I took a look at the API doc, but that method doesn't create objects, that just serialize existing objects. – Simple Apr 01 '18 at 00:09
0

As far as I know you need to get the whole object then query it's ID. I don't see a query id method here https://github.com/sidiabale/parse4cn1/blob/41fe491699e604fc6de46267479f47bc422d8978/src/com/parse4cn1/ParseQuery.java

Shai Almog
  • 51,749
  • 5
  • 35
  • 65