Support Topics Documentation Slack YouTube Blog

Have row of data updated

Hi. I’m having a bit of problem with storing the URL’s for my users images. Not sure if I’m doing it wrong or if it’s meant to be like this. But each time my user uploads an image, a new row is created in the data table:

http://support.backendless.com/public/attachments/079d8f2d8d453f2cba6b7dd1e912ac67.PNG</img>

As you can see, it belongs to the same user (me) but it creates a new row with a new ObjectID each time. This builds up to LOADS of rows of data being created in my console, since even when the user deletes an image and hence it’s URL too, and continue to upload a new one afterwards, a new row is created. I find this “wasteful” or wrong. I would want a single row of this data table per user and simply update the different URL’s “url_9_3” etc… without creating a new row.

Is this what should be done if to be correct? Or is it meant to create new rows everytime new data is being applied to that table?

Hope I made some sense in my Q.

Thanks in advance.

Hello

To avoid adding new records you need to retrieve the object first and then to make changes to it.

Regards Anton

Hello Anton. Thanks for replying. Can you elaborate in the form of some psuedo code?

My code: (this is called on the “handleResponse” method of the image uploading method)

Backendless.Data.of(UserFileMapping.class).save(fileMapping, new AsyncCallback&lt;UserFileMapping&gt;() {
    @Override
    public void handleResponse(UserFileMapping response) {
        
        //toast_error("Image stored");
        System.out.println("UPLOADED!" + LocalPreferences.getImageURL("2","_1",MemoriesActivity.this));
    }

    @Override
    public void handleFault(BackendlessFault fault) {
        Toast toast = Toast.makeText(getApplicationContext(),"An error occurd, try again.",Toast.LENGTH_SHORT);
        toast.show();
        System.out.println("ERROR" + fault.getCode());
    }
});

I tried this based on your reply:

Backendless.Persistence.of(UserFileMapping.class).find(new AsyncCallback&lt;BackendlessCollection&lt;UserFileMapping&gt;>() {
    @Override
    public void handleResponse(BackendlessCollection&lt;UserFileMapping&gt; response) {
        Backendless.Data.of(UserFileMapping.class).save(fileMapping, new AsyncCallback&lt;UserFileMapping&gt;() {
            @Override
            public void handleResponse(UserFileMapping response) {
                Toast.makeText(getApplicationContext(),"Finished!",Toast.LENGTH_SHORT).show();
                System.out.println("UPLOADED!" + LocalPreferences.getImageURL("2","_1",MemoriesActivity.this));
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                Toast.makeText(getApplicationContext(),"An error occurd, try again.",Toast.LENGTH_SHORT).show();
                System.out.println("ERROR" + fault.getCode());
            }
        });
    }

    @Override
    public void handleFault(BackendlessFault fault) {
        Toast.makeText(getApplicationContext(),"Failed to retrieve data from server",Toast.LENGTH_SHORT).show();
    }
});

But it did not work either, still creates a new row of data. Am I doing it wrong?

You’re saving some “fileMapping” object, which most probably doesn’t contain objectId. And when you save an object without an objectId value, the new row is created.

So you suggest I save the objectID when the user saves the fileMapping url’s for the first time, and use that ID for future save’s ?

Yes, sure. ObjectID is the field by which we identify the same objects, so that when you call “save” we know which one you want to update.

You can store them as you just said, or you can retrieve the appropriate object by some criteria and update that object since it will automatically contain the appropriate objectId.

Okey I think I get the concept. But I’m still not sure how I can get hold of the ObjectID being created on the first save, so I can store it in my data table for future searches. There’s no .getObjectID() anywhere? Can I use the DataQuery with WhereClause to find the objectID?

I solved it without bothering with objectID. Would gladly appreciate it if some Admin here would just confirm that this is a proper way of doing it? I’ll mark this as the answer, but just a tiny comment underneath saying if it’s ok or not would be great.
Here’s how:

Backendless.Persistence.of(UserFileMapping.class).find(new AsyncCallback&lt;BackendlessCollection&lt;UserFileMapping&gt;>() {
                    @Override
                    public void handleResponse(BackendlessCollection&lt;UserFileMapping&gt; response) {
                        if(response.getCurrentPage().size() > 0){
                            UserFileMapping fileMapping = response.getCurrentPage().get(0);
 
                            fileMapping.url_1 = tempURL;
                            Backendless.Persistence.of(UserFileMapping.class).save(fileMapping, new AsyncCallback&lt;UserFileMapping&gt;() {
                                @Override
                                public void handleResponse(UserFileMapping response) {
 
                                }
 
                                @Override
                                public void handleFault(BackendlessFault fault) {
 
                                }
                            });
 
                        }
 
                    }
 
                    @Override
                    public void handleFault(BackendlessFault fault) {
 
                    }
                });

What was the AsynCallback&lt in your code.
LocalPreferences indicates what.
I found it completely confusing will u please help me with that

The AsyncCallback class is part of Backendless SDK. It is documented here:
https://backendless.com/docs/android/sync_and_async_calls.html

Backendless.Persistence.of(UserFileMapping.class).find(new AsyncCallback<BackendlessCollection<UserFileMapping>>() {
@Override
public void handleResponse(BackendlessCollection<UserFileMapping> response) {
if(response.getCurrentPage().size() > 0){
UserFileMapping fileMapping = response.getCurrentPage().get(0);

                        fileMapping.url_1 = tempURL;

Yes,i know about that one but i am not getting what he has done there.
I have a problem while understanding it .

That post is 3 years old. Some of the referenced classes, such as BackendlessCollection are no longer in the SDK.

However, what the code does is retrieves a collection of objects, gets the first one from the collection, modifies it and saves it back in the database.

Regards,
Mark