Backendless Support
 
Solved

Saving EXISTING object gets FAULT = 'Server.Processing' exception during method invocation

Hi, I'm aware of several other threads relating to the server.processing exception during method invocation issue. Most of those issues were related to people not adding relations in the new way required in 4.x. I have a different issue in that I'm getting this fault when trying to save an existing object.

I shouldn't have to do a setrelation again on an existing object do I? I really hope not because that would seem extremely backwards to me.

Steps to reproduce:

1) Save object that has a 1:1 relation with BackendlessUser

2 ) Set the relation after saving the object

3) Save the existing object with the user relation set (just save with the object you get from a find)

4) BOOM fault

Observations.

Currently, if I go and set the user field to nil right before I save the existing object again, it goes through and I have to do another setrelation again. This is definitely a dangerous workaround as now one step save has turned into 2 where if something happens to the network in between, I'll potentially have an orphaned object with no user relation.

Please advise. I'm on 4.0.10 iOS SDK.

Thanks!

Leave a Comment

Comments (13)

photo
1

Hi Allen,

Since the version 4 was released you have to set relations between tables using separate methods (setRelation, addRelation, deleteRelation). It is the only way to manage relations.

Here you can find the documentation.

Regards Ilya

photo
1

Thanks Ilya,

I'm aware of the need to setrelations in version 4. I am doing so for all NEWLY created objects. What the docs don't seem to mention is how and if this applies to updating or saving EXISTING objects.

For example say I have an object Post with a 1:1 relation to BackendlessUser named author.

1) I create and save Post

2) I create and save BackendlessUser

3) Set relation between post and author

4) Change some content on Post

5) How am I supposed to save the changes to Post now? It exists, the relation is set and the author field is populated with an instance of BackendlessUser

Currently, if I just do a save at step 5, I get the server.processing response. Is this somehow the expected behavior?

Am I seriously supposed to set the author to nil before doing an update/save and setrelation AGAIN?

photo
1

I see that I misunderstood you firstly, sorry.

Let me check if I got everything properly this time.

- You get an exception when trying to update existing object Post with not empty field 'author'.

- When you set it to null before saving, everything works properly but you afraid it will remove existing relation

Is it correct?

photo
1

Yes that's correct Ilya, thanks. I wanted to clarify what exactly is the proper way to handle this or basically how it was designed to work in the first place, so I can understand what the right way is going about it or if this is an actual bug.

Thanks!

photo
1

The system is designed to complete ignore any related properties when you either initially save an object or update an existing object. All operations managing the state of a relation are done exclusively via dedicated methods (add/set/removeRelation).

Regards,

Mark

photo
1

Thanks for the reply Mark. That's how I assumed it was designed to work. This means the fault response should be a bug correct?

photo
1

Yes, it should not bomb out. I will open a ticket to verify this behavior.

Regards,

Mark

photo
1

Thanks!

photo
photo
1

Hello Allen,

Could you please provide your code sample because everything works fine for me.

Here are my tests:

  1. id<IDataStore>postStore = [backendless.data of:[Post class]];
  2. id<IDataStore>userStore = [backendless.data of:[BackendlessUser class]];

Sync (results on screenshot1)

  1. // Creating a new Post
  2. Post *post = [Post new];
  3. post.name = @"Post1";
  4. Post *savedPost = [postStore save:post];
  5. NSLog(@"New Post saved: %@", savedPost.name);
  6. // Creating a new Backendless User
  7. BackendlessUser *user = [BackendlessUser new];
  8. user.name = @"User1";
  9. user.email = @"user1@test.com";
  10. user.password = @"111";
  11. BackendlessUser *savedUser = [userStore save:user];
  12. NSLog(@"New Backendless User saved: %@", savedUser.name);
  13. // Setting the relation
  14. NSString *postObjectId = [backendless.data getObjectId:post];
  15. NSNumber *setRelation = [postStore setRelation:@"author" parentObjectId:postObjectId childObjects:@[user.objectId]];
  16. NSLog(@"Relation has been set: %@", setRelation);
  17. // Updating Post
  18. post.name = @"Upated Post1";
  19. [postStore save:post];
  20. NSLog(@"Post updated: %@", post.name);

Async (results on screenshot2)

  1. // Creating a new Post
  2. Post *post = [Post new];
  3. post.name = @"Post2";
  4. [postStore save:post
  5. response:^(Post *savedPost) {
  6. NSLog(@"New Post saved: %@", savedPost.name);
  7. } error:^(Fault *fault) {
  8. NSLog(@"Error: %@", fault.message);
  9. }];
  10. // Creating a new Backendless User
  11. BackendlessUser *user = [BackendlessUser new];
  12. user.name = @"User2";
  13. user.email = @"user2@test.com";
  14. user.password = @"111";
  15. [userStore save:user
  16. response:^(BackendlessUser *savedUser) {
  17. NSLog(@"New Backendless User saved: %@", savedUser.name);
  18. } error:^(Fault *fault) {
  19. NSLog(@"Error: %@", fault.message);
  20. }];
  21. // Setting the relation
  22. Post *post = [postStore findFirst];
  23. BackendlessUser *user = [userStore findFirst];
  24. NSString *postObjectId = [backendless.data getObjectId:post];
  25. [postStore setRelation:@"author"
  26. parentObjectId:postObjectId
  27. childObjects:@[user.objectId]
  28. response:^(NSNumber *setRelation) {
  29. NSLog(@"Relation has been set: %@", setRelation);
  30. post.name = @"Upated Post2";
  31. [postStore save:post
  32. response:^(Post *post) {
  33. NSLog(@"Post updated: %@", post.name);
  34. }
  35. error:^(Fault *fault) {
  36. NSLog(@"Error: %@", fault.message);
  37. }];
  38. } error:^(Fault *fault) {
  39. NSLog(@"Error: %@", fault.message);
  40. }];

Regards, Olga

photo
1

Thanks for the testing Olga, I've been banging my head against my screen and can't figure out why this is happening.

I created a new table Post specifically to test against this. I ended up making it almost mirror the schema of the other table that has been giving me the fault. I run the following code below and for the Post class it goes through fine, but for the Shorti class it gives me the fault. Can you verify if you see the same thing? And what would make one fault and the other not when the schema's are identical?

You can test against my app: 08D73C6A-C7AB-BAD9-FFEA-DBFAAD5AEA00 I've attached the Shorti and Post classes I'm using.

  1. id<IDataStore> postStore = [backendless.data of:[Shorti class]];
  2. id<IDataStore> userStore = [backendless.data of:[BackendlessUser class]];
  3. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  4. @try{
  5. // Creating a new Post
  6. Shorti *post = [Shorti new];
  7. post.descrip = @"Post1";
  8. Shorti *savedPost = [postStore save:post];
  9. NSLog(@"New Post saved: %@", savedPost.descrip);
  10. // Creating a new Backendless User
  11. BackendlessUser *user = [BackendlessUser new];
  12. user.name = @"User1";
  13. user.email = @"user1@test.com";
  14. user.password = @"111";
  15. BackendlessUser *savedUser = [userStore save:user];
  16. NSLog(@"New Backendless User saved: %@", savedUser.name);
  17. // Setting the relation
  18. NSString *postObjectId = savedPost.objectId;
  19. NSNumber *setRelation = [postStore setRelation:@"user" parentObjectId:postObjectId childObjects:@[savedUser.objectId]];
  20. NSLog(@"Relation has been set: %@", setRelation);
  21. // Updating Post
  22. savedPost.descrip = @"Updated Post1";
  23. savedPost = [postStore save:savedPost];
  24. NSLog(@"Post updated: %@", savedPost.descrip);
  25. DataQueryBuilder *queryBuilder = [[DataQueryBuilder alloc] init];
  26. [queryBuilder setWhereClause:[NSString stringWithFormat:@"user.objectId = '%@'", savedUser.objectId]];
  27. [queryBuilder setRelated:@[@"user"]];
  28. [queryBuilder setRelationsDepth:1];
  29. [queryBuilder setSortBy:@[@"created DESC"]];
  30. [queryBuilder setPageSize:100];
  31. NSArray<Shorti*> *result = [postStore find:queryBuilder];
  32. Shorti *existingPost = [result firstObject];
  33. existingPost.descrip = @"Updated again!";
  34. [postStore save:existingPost];
  35. NSLog(@"Updated again!");
  36. } @catch(Fault *fault){
  37. NSLog(@"FAULT: %@", fault);
  38. }
  39. });

Thanks!

photo
1

Hello Allen,

We'll investigate this issue and answer you as soon as possible.

Regards, Olga

photo
1

Hi Allen,

We've investigated the problem and found that it's related to the object with cyclic relations being sent to the afterFind handler. The task with ID BKNDLSS-15799 has been created and we'll get back to you when we have any further news.

photo
1

Hi Allen,

The problem has been fixed.