Backendless Support
 
Solved

User relations being cleared after loading more relations - iOS

None of my relations are on autoload, for efficiency's sake. I am loading relations in rounds, getting some in the first round (in the viewDidLoad) and moving on to round 2 of loading relations after the view has loaded. My code to retrieve the first set of relations is as follows:

  1. Fault *fault = nil;
  2. BackendlessDataQuery *query = [BackendlessDataQuery new];
  3. query.whereClause = [NSString stringWithFormat:@"objectid = \'%@\'", userToDisplay.objectId];
  4. query.queryOptions.related = [NSMutableArray arrayWithArray:@[@"relation1", @"relation2", @"relation3.relation1-of-relation3"]];
  5. NSArray *userData = [[backendless.persistenceService of:BackendlessUser.class] find:query fault:&fault].data;
  6. userToDisplay = [userData objectAtIndex:0];

This is working fine, and userToDisplay now references a BackendlessUser object with the specified relations.

The second round of loading is as follows:

  1. [backendless.persistenceService
  2. load:userToDisplay
  3. relations:@[@"relation3.relation2-of-relation3", @"relation3.relation3-of-relation3", @"relation4.*"]
  4. response:^(id response)
  5. {
  6. userEvents = [self getUserEventsWithUser: userToDisplay];
  7. }
  8. error:^(Fault *fault)
  9. {
  10. NSLog(@"Fault: %@", fault.description);
  11. }];

This is where the problem starts. Now, userToDisplay is a reference to a BackendlessUser object with relation1, relation2, and relation3, but only the sub-relations relation3.relation2-of-relation3 and relation3.relation3-of-relation3.

relation3.relation1-of-relation3, which I retrieved in the first round of loading, is now empty. This is a problem because I do not want to have to load it again because I already did, but if I don't, it is empty.

Is there any way to add on more sub-relations to a user without emptying the sub-relations that aren't specified in the second load?

Thanks

Best Answer
photo

You can merge the existing properties with loaded properties of user, for example:

  1. -(void)mergingUser:(BackendlessUser *)user withRelations:(NSArray<NSString*> *)relations {
  2. [backendless.persistenceService
  3. load:user
  4. relations:relations
  5. response:^(id response) {
  6. BackendlessUser *loadedUser = (BackendlessUser *)response;
  7. NSDictionary<NSString*, id> *properties = [user retrieveProperties];
  8. // merge the user properties
  9. [loadedUser addProperties:properties];
  10. NSLog(@"%@", loadedUser);
  11. }
  12. error:^(Fault *fault) {
  13. NSLog(@"%@", fault);
  14. }];
  15. }

Leave a Comment

Comments (2)

photo
1

You can merge the existing properties with loaded properties of user, for example:

  1. -(void)mergingUser:(BackendlessUser *)user withRelations:(NSArray<NSString*> *)relations {
  2. [backendless.persistenceService
  3. load:user
  4. relations:relations
  5. response:^(id response) {
  6. BackendlessUser *loadedUser = (BackendlessUser *)response;
  7. NSDictionary<NSString*, id> *properties = [user retrieveProperties];
  8. // merge the user properties
  9. [loadedUser addProperties:properties];
  10. NSLog(@"%@", loadedUser);
  11. }
  12. error:^(Fault *fault) {
  13. NSLog(@"%@", fault);
  14. }];
  15. }

photo
1

Thank you, works like a charm.