Hi guys,
I have an object “Photo” with a relation of “Comment” objects.
When I write the first comment “Hello, this is first comment” for an existing photo, all goes well and a Photo is updated with a new comment and a new “Comment” is created.
When I write another comment “And this is second comment” for the same photo, I find 3 comments in my “Comment” table. The first comment is duplicate so I have:
1- Hello, this is first comment
2- Hello, this is first comment
3- And this is second comment
And so on…
What I am doing wrong?
The code is:
// Create a new comment object and add it to the photo relation
Comment *comment = [[Comment alloc]init];
[comment setText:text];
[comment setUsername:name];
[photo addComment:comment];
// This is the code for “addComment”
-
(void)addComment:(Comment *)comment {
if (!self.comments) {
self.comments = [NSMutableArray array];
}
[self.comments addObject:comment];
}
// This is the code to save object photo (and comment too)
[[backendless.persistenceService of:[Photo class]] save:photo];
Hi Francesco,
When you save your photo with a comment, the comment will be updated with objectId assigned to it. When you re-save the photo with a collection of comments, the backend will check if the comment object has objectId value. If it does, the backend performs update, otherwise a new one is created.
Check the response for the save operation. It should include the comments with objectId assigned to them.
Regards,
Mark
Ok, you’re right and I was forgetting this step.
But what about a user who writes a comment and then adds a new comment just a second later? This is a possible scenario…
That’s not a problem. You can always save the comment object directly in the Comment table. Once you know it’s objectID, you can add that object to the photo and save the photo.
You are right. The only thing is that I have to complete the entire process in two steps but it’s not a really problem
I will try…
Alternatively, you could have an internal flag that a save operation (for the photo) is in progress. In that case, you’d wait till it comes back before you save the next comment.
I used your first approach and it works.
Thank you and sorry for the (maybe) dumb question