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:
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?
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.
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<BackendlessCollection<UserFileMapping>>() {
@Override
public void handleResponse(BackendlessCollection<UserFileMapping> 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<UserFileMapping>() {
@Override
public void handleResponse(UserFileMapping response) {
}
@Override
public void handleFault(BackendlessFault fault) {
}
});
}
}
@Override
public void handleFault(BackendlessFault fault) {
}
});
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.