Backendless Support
 
Waiting for user's response

How to determine if a relation has already been retrieved?

I need to load the current user's friends. I don't want to retrieve the friends if I have already retrieved them so I am trying to test to see if I have. If I haven't already retrieved the friends, I will do so. The code I am using right now to do this is as follows:

  1. currentUser = [backendless.userService currentUser];
  2. NSArray *localFriends = [currentUser getProperty:@"friends"];
  3. if (localFriends == nil)
  4. {
  5. BackendlessUser *loaded = [backendless.persistenceService load:currentUser relations:@[@"friends"]];
  6. NSDictionary<NSString*, id> *loadedProperties = [loaded retrieveProperties];
  7. [currentUser addProperties:loadedProperties];
  8. localFriends = [currentUser getProperty:@"friends"];
  9. }
  10. friends = localFriends;

This doesn't work because the NSArray localFriends isn't nil even when "friends" has never been retrieved. It is just an NSArray with 0 objects. I could test to see if the array's count is 0, but what if the user just has 0 friends? I need some help to figure out how to check to see if I have previously loaded the friends property and added it to the currentUser or not. Thanks

Leave a Comment

Comments (6)

photo
1

Hi Cooper,

The NSArray property should be nil in case you haven't retrieved a relation.

Are you sure that friends really haven't been retrieved? Maybe you specified relationsDepth somewhere when retrieving the user?

photo
1

The relation is not nil, it is an NSArray object with 0 elements, by default. To make sure that I am not accidentally retrieving the user with relationsDepth, I logged the user object upon login and examined it. All of its relations are arrays with 0 objects.

photo
photo
1

Hi Cooper,

Please, provide your appId here or to support@backendless.com. We will investigate this issue with your data.

Regards,

Slava

photo
1

App ID: C1C73839-75FD-DF58-FF72-2CBCEA0F9100

photo
photo
1

If you need to have the current user friends just after login, you should switch on a "friends" checkbox:

ba1eb2aa1cce0df3921c09cfc9ba00ff

photo
1

I do need to have the friends of the user, but I do not have the autoload checked because I don't want every user object that I retrieve to load its friends right away. Would this not significantly increase the time it takes to load each user object? That is my only worry and it is the reason I do not use autoload at all.

photo