Backendless Support
 
Answered

Linking Related data using client API

Hi,

I have the following scenario which need your help please

Using the console we can update link to related items in other tables ,, but as far I as know the same thing can’t be done using the client API..

Take this scenario for example :

If I have two tables “peopleNames” and “peopleContacts” and I have “Contact” Field to store 1-1 reference to “peopleContacts” in “peopleNames” Table …. now today If I need to link an item from “PeopleContacts” then first I have to query “PeopleContacts” for the object and then update the Contact” Field in “peopleNames” with it ..this is fine but what if I already have the objectId of the record in “peopleContacts” can I just link it without updating or saving in “peopleContacts” ?

I couldn't see anyway where I just link that “Contact” object without Querying “peopleContacts” records for that ObjectId and without the need to save or update the record in “peopleContacts” table ..

Now if there is no way but to run that query first I have two issues with that :

1- It’s additional delay as I have to query for the whole Record before being able to update the “peopleNames” record when all I need is just to link that Object by objectId

2-There could be some updates on that same peopleContacts” record after I queried the table but before I actually update the “peopleNames” with the Related Contact Object .. This means with my save or update API call it will actually update all those fields for the Contact record in the “peopleContacts” table .. which mean overwriting some recent changes here also .

Because of the above issues, I thought there could be a way to just link a related object if we know the ObjectId …something similar to what we do in console today

am I missing anything here ?

Note: Im using iOS

Regards

Eyad

Leave a Comment

Comments (10)

photo
1

Hello.

When you "just" link one object to another you do change that object. And you should save it.

Nevertheless you able switch off autoload relations and retrieve them as needed.

It is well described in documentation.

https://backendless.com/documentation/data/ios/data_relations_save_update.htm

photo
1

You also can set pagesize to 1 if you don't want to load many related objects.

photo
1

Eyad,

If you already know "parent" objectId, you do not need to lookup that object. You should be able to create an instance of "peopleNames" class, populate its objectId, assign the related object and save that peopleNames object. It should work, please give it a try and let us know.

Regards,

Mark

photo
1

Hi Mark,

Thanks for you reply .. Yes I did that and but I couldn't save the Object by knowing its ObjectId without overwriting the other values of that Object .. so I guess what I need is a away to update the links in both tables without touching the other values of the Related Object .. is that possible ?

Thanks

Eyad

photo
1

Hi Eyad,

It should be possible if you use the Dictionary approach - that is persisting your object as a dictionary rather than an instance of a specific class. Do you need an example for Obj-C or Swift?

Regards,

Mark

photo
1

Yes please Obj C

photo
1

Here's an example of saving an object into the "Contact" table:

  1. NSDictionary *contact = @{
  2. @"name": @"Jack Daniels",
  3. @"age": @(147),
  4. @"phone": @"777-777-777",
  5. @"title": @"Favorites"
  6. };
  7. // sync
  8. Fault *error = nil;
  9. contact = [[backendless.data ofTable:@"Contact"] save:contact fault:&error];
  10. if (!error) {
  11. NSLog(@"Data has been saved (SYNC): %@", contact[@"objectId"]);
  12. }
  13. else {
  14. NSLog(@"Server reported an error: %@", error);
  15. }
  16. // async
  17. [[backendless.data ofTable:@"Contact"] save:contact
  18. response:^(NSDictionary<NSString*,id> *contact) {
  19. NSLog(@"Data has been saved (ASYNC): %@", contact[@"objectId"]);
  20. }
  21. error:^(Fault *fault) {
  22. NSLog(@"importGroups: %@", fault);
  23. }];

In your case, you would put the "objectId" property in the "parent" object as well as a property for the relation. The related object can also be NSDictionary. Make sure to put the "___class" property with the name of the related table into the related dictionary.

Regards,

Mark

photo
1

Hello Mark,

Brilliant it worked for the save API (backendless.persistenceService save: entity: error:)

but it failed for the update API (backendless.persistenceService update: entity: sid: error:)

I have used the same entity and table name for both scenarios , I just added the objectId in the “sid” field for the Update API

The error message I received is :”Entity with the specified ID cannot be found: Id - missing id”

any idea what is wrong here ?

Note: Im using SDK 3.0.44

Thanks

Eyad

photo
1

An Update ...

the update API is working now , apparently in addition to including the objectId in the "sid" flag , also we need to add the "objectId" key in the entity dictionary .

Just a suggestion.. Maybe these details about iOS platform examples to save Data Object as Dictionary items need to be documented somewhere (maybe its there but I couldn't find it )

Many thanks Mark for your quick help and guidance

Regards

Eyad

photo
1

Eyad,

I am glad it worked for you. The docs are in progress - we updated the Android API about the addition of the dictionary-based support and the iOS are next.

Regards,

Mark

photo