Backendless Support
 
Solved

Unable to update an object (iOS)

I try to update an object by using whereClause to find it and by creating NSDictionary to update it, but it gives me errors.

NSString *whereClause = [NSString stringWithFormat:@"atitle = '%@'", atitle];

DataQueryBuilder *queryBuilder = [[DataQueryBuilder alloc] init];

[queryBuilder setWhereClause:whereClause];

id<IDataStore>dataStore = [backendless.data ofTable:@"menuTable"];

[dataStore find:queryBuilder

response:^(NSArray<NSDictionary<NSString *, id> *> *foundMenu2) {

NSString *obID = [NSString stringWithFormat:@"%@",[foundMenu2 valueForKey:@"objectId"]];

NSDictionary *menuNew = @{

@"objectID": obID,

@"course_1": _textField.text

};

id<IDataStore> dataStoreSave = [backendless.data ofTable:@"menuTable"];

[dataStoreSave save:menuNew

response:^(NSDictionary<NSString*, id> *contact) {

NSLog(@"Contact saved");

}

error:^(Fault *fault) {

NSLog(@"Server save reported an error: %@", fault);

}

];

}

error:^(Fault *fault) {

NSLog(@"Server reported an error: %@", fault);

}

];

The first error (when I use @"objectID" in the NSDictionary): FAULT = '1003' [Cannot persist object. Property 'objectId' must be set by the server. Make sure the property is not initialized when saving new object.] <Cannot persist object. Property 'objectId' must be set by the server. Make sure the property is not initialized when saving new object.>

I do totally understand what does this error say but I can not understand how can I update an object without specifying it's objectId.

The second error (when I use @"objectId" in the NSDictionary): FAULT = '1000' [Entity with name (

"49B27B10-F9DB-B0CB-FFE5-D65970D16900"

) cannot be found] <Entity with name (

"49B27B10-F9DB-B0CB-FFE5-D65970D16900"

) cannot be found>

Best Answer
photo

Hello Vasilii,

Please, check this code:

  1. NSString *whereClause = [NSString stringWithFormat:@"atitle = '%@'", atitle];
  2. DataQueryBuilder *queryBuilder = [[DataQueryBuilder alloc] init];
  3. [queryBuilder setWhereClause:whereClause];
  4. id<IDataStore>dataStore = [backendless.data ofTable:@"menu"];
  5. [dataStore find:queryBuilder
  6. response:^(NSArray<NSDictionary<NSString*, id> *> *menus) {
  7. // Here we get all menus which match queryBuilder where clause
  8. // According to your example we have only one record which matches our where clause
  9. NSDictionary *menuToChange = [NSDictionary new];
  10. if (menus.count == 1) {
  11. menuToChange = menus[0];
  12. }
  13. // Changing the menu:
  14. [menuToChange setValue:_textField.text forKey:@"course_1"];
  15. // Saving changes:
  16. [dataStore save:menuToChange
  17. response:^(NSDictionary<NSString*, id> *changedMenu) {
  18. NSLog(@"Menu's course_1 changed to: %@", [changedMenu valueForKey:@"course_1"]);
  19. }
  20. error:^(Fault *fault) {
  21. NSLog(@"Server reported an error: %@", fault.message);
  22. }];
  23. }
  24. error:^(Fault *fault) {
  25. NSLog(@"Server reported an error: %@", fault.message);
  26. }];

Hope this helps.

By the way the latest version of SDK is 4.0b8, you can check release history here.

Regards, Olga

Leave a Comment

Comments (11)

photo
1

Have you checked in the debugger if the following returns a valid objectId?:

NSString *obID = [NSString stringWithFormat:@"%@",[foundMenu2 valueForKey:@"objectId"]];

photo
1

Mark, yes. It returns me: 49B27B10-F9DB-B0CB-FFE5-D65970D16900. The same objectId has an object (the lowest one) which corresponds to my whereClause query.cb3c3507d1d413276e3750d5f0b49192

photo
1

Could you try changing the following line:

  1. @"objectID": obID,

to

  1. @"objectId": obID,

photo
1

Mark, yes, I've already tried it. It gives me the second error:

Server save reported an error: FAULT = '1000' [Entity with name (

"49B27B10-F9DB-B0CB-FFE5-D65970D16900"

) cannot be found] <Entity with name (

"49B27B10-F9DB-B0CB-FFE5-D65970D16900"

) cannot be found>

photo
1

Do you happen to have any event handlers in the app (cloud code)?

photo
1

Mark, no, I use only Data

photo
1

If it is important, I've renamed the table many times (3 or 4). It was "menu" then I renamed it to "menuTable" and wanted to change it back to "menu", but even the console gave me the red message error (when I selected the table) that the table with the name "menu" does not exist. So I renamed it back to "menuTable" and I didn't face any errors messages

photo
1

Thanks. I opened an internal ticket to investigate the problem (BKNDLSS-14859). Meanwhile, could you please check what version of the SDK library you use?

photo
1

Mark, I've just updated the pod to 4.0b6 today

photo
photo
1

Hello Vasilii,

Please, check this code:

  1. NSString *whereClause = [NSString stringWithFormat:@"atitle = '%@'", atitle];
  2. DataQueryBuilder *queryBuilder = [[DataQueryBuilder alloc] init];
  3. [queryBuilder setWhereClause:whereClause];
  4. id<IDataStore>dataStore = [backendless.data ofTable:@"menu"];
  5. [dataStore find:queryBuilder
  6. response:^(NSArray<NSDictionary<NSString*, id> *> *menus) {
  7. // Here we get all menus which match queryBuilder where clause
  8. // According to your example we have only one record which matches our where clause
  9. NSDictionary *menuToChange = [NSDictionary new];
  10. if (menus.count == 1) {
  11. menuToChange = menus[0];
  12. }
  13. // Changing the menu:
  14. [menuToChange setValue:_textField.text forKey:@"course_1"];
  15. // Saving changes:
  16. [dataStore save:menuToChange
  17. response:^(NSDictionary<NSString*, id> *changedMenu) {
  18. NSLog(@"Menu's course_1 changed to: %@", [changedMenu valueForKey:@"course_1"]);
  19. }
  20. error:^(Fault *fault) {
  21. NSLog(@"Server reported an error: %@", fault.message);
  22. }];
  23. }
  24. error:^(Fault *fault) {
  25. NSLog(@"Server reported an error: %@", fault.message);
  26. }];

Hope this helps.

By the way the latest version of SDK is 4.0b8, you can check release history here.

Regards, Olga

photo
1

Olga, thank you! It works!

photo