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

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

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

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

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

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

Yes please Obj C

Here’s an example of saving an object into the “Contact” table:

    NSDictionary *contact = @{
                              @"name": @"Jack Daniels",
                              @"age": @(147),
                              @"phone": @"777-777-777",
                              @"title": @"Favorites"
                              };
     
    // sync
    Fault *error = nil;
    contact = [[backendless.data ofTable:@"Contact"] save:contact fault:&error];
    if (!error) {
        NSLog(@"Data has been saved (SYNC): %@", contact[@"objectId"]);
    }
    else {
        NSLog(@"Server reported an error: %@", error);
    }
     
    // async
    [[backendless.data ofTable:@"Contact"] save:contact
          response:^(NSDictionary<NSString*,id> *contact) {
              NSLog(@"Data has been saved (ASYNC): %@", contact[@"objectId"]);
          }
          error:^(Fault *fault) {
              NSLog(@"importGroups: %@", fault);
          }];

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

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

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

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