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:
currentUser = [backendless.userService currentUser];
NSArray *localFriends = [currentUser getProperty:@"friends"];
if (localFriends == nil)
{
BackendlessUser *loaded = [backendless.persistenceService load:currentUser relations:@[@"friends"]];
NSDictionary<NSString*, id> *loadedProperties = [loaded retrieveProperties];
[currentUser addProperties:loadedProperties];
localFriends = [currentUser getProperty:@"friends"];
}
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
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?
Hi Cooper,
Please, provide your appId here or to support@backendless.com. We will investigate this issue with your data.
Regards,
Slava
If you need to have the current user friends just after login, you should switch on a “friends” checkbox:
http://support.backendless.com/public/attachments/ba1eb2aa1cce0df3921c09cfc9ba00ff.png</img>
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.
App ID: C1C73839-75FD-DF58-FF72-2CBCEA0F9100
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.