Hello,
The logic I’m trying to implement here is, when you receive a friend request, whether you accept or reject it, the request should be removed
Calling the following method
+ (void)rejectFriendRequest:(NSString *)userID {
Users *tempUser;
for (Users *user in [DataService sharedInstance].currentUser.friendsRequest) {
if ([user.objectId isEqualToString:userID]) {
tempUser = user;
break;
}
}
[[DataService sharedInstance].currentUser.friendsRequest removeObject:tempUser];
id<IDataStore> dataStore = [backendless.persistenceService of:[Users class]];
[dataStore save:[DataService sharedInstance].currentUser response:^(id responseBlock){
[[DataService sharedInstance].strangeUsers addObject:[DataService findUserByID:userID]];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AppUserUpdate" object:nil];
} error:^(Fault *fault){
NSLog(@"%@", fault.detail);
}];
// [[backendless.persistenceService of:[BackendlessUser class]] save:[DataService sharedInstance].currentUser response:^(id response) {
//
// [[DataService sharedInstance].strangeUsers addObject:[DataService findUserByID:userID]];
// [[NSNotificationCenter defaultCenter] postNotificationName:@"FriendRequestRejected" object:nil];
//
// } error:^(Fault *fault) {
//
// NSLog(@"%@", fault.detail);
// }];
}
Either using the datastore way or the commented way, I receive the response in attached image.
As you can see the service returns a user with it’s friendRequests decreased in a correct manner, hence I thought this was over for me.
However when I restart the app, reload the user or go check on the web client, I find that the user’s friendRequests are still intact !
Know that the following method works perfectly for sending out friend requests (or adding to it)
+ (void)sendFriendRequest:(NSString *)userID {
Users *tempUser = [DataService findUserByID:userID];
[tempUser.friendsRequest addObject:[DataService sharedInstance].currentUser];
[[backendless.persistenceService of:[BackendlessUser class]] save:tempUser response:^(id response) {
[[DataService sharedInstance].strangeUsers removeObject:tempUser];
[[DataService sharedInstance].sentFriendRequests addObject:tempUser];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AppUserUpdate" object:nil];
} error:^(Fault *fault) {
NSLog(@"%@", fault.detail);
}];
}
And also the following method accepts friend requests correctly, but it also can’t remove the related friend request, giving out the same deceiving response as the first “reject” method
+ (void)acceptFriendRequest:(NSString *)userID {
Users *tempUser = [DataService findUserByID:userID];
[[DataService sharedInstance].currentUser.friends addObject:tempUser];
[[backendless.persistenceService of:[BackendlessUser class]] save:[DataService sharedInstance].currentUser response:^(id response) {
[[DataService sharedInstance].appUsers removeObject:tempUser];
[[DataService sharedInstance].justFriends addObject:tempUser];
Users *tempFriendRequest;
for (Users *user in [DataService sharedInstance].currentUser.friendsRequest) {
if ([user.objectId isEqualToString:userID]) {
tempFriendRequest = user;
break;
}
}
[[DataService sharedInstance].currentUser.friendsRequest removeObject:tempFriendRequest];
id<IDataStore> dataStore = [backendless.persistenceService of:[Users class]];
[dataStore save:[DataService sharedInstance].currentUser response:^(id responseBlock){
[[NSNotificationCenter defaultCenter] postNotificationName:@"AppUserUpdate" object:nil];
} error:^(Fault *fault){
NSLog(@"%@", fault.detail);
}];
} error:^(Fault *fault) {
NSLog(@"%@", fault.detail);
}];
}
App ID: 310FB739-738B-ABD0-FF01-83B62265E300
Hi Ahmed,
In order to remove the related object from collection, you remove it from the collection in your code and then save the parent object with the updated collection. This is also described here: https://backendless.com/documentation/data/ios/data_relations_delete.htm
If that does not work, please prepare a self-contained code sample which we could run without configuring much and see that there is a problem. Note that debugging your application’s code is not free, according to our Support Policy.
Hello Sergey,
I attached a mini working version of my app for you to see the problem. Just download, uncompress, pod install and run.
Register two users to be able to send a friend request from one and see it on the other one.
All the method calls with Backendless you’ll need to check are in BackendlessService.m, in the Service folder.
Using the app,
To send a friend request, from the home page go to AppUsers -> Add Friend (for any listed user)
To see a friend request, from the home page go to AppUsers
I manually programmed the app to not display accepted friend requests for existing friends, in case you were wondering about that. Not a pretty fix I know.
Many thanks for your great support. I’m aware you don’t freely debug applications code, but I wasn’t expecting you to debug my code as it already runs successfully.
tobackendlesssupport.zip (2.31MB)
Thanks for preparing a sample, I’ve created an internal task to investigate your issue. For reference, the task ID is BKNDLSS-12703.
You shouldn’t use ‘Users’ name for custom class because this name is used as inside Backendless class (table name). You MUST rename ‘Users’ class in your project.
I tried changing Users in my iOS project to AppUser, and got the following error upon trying to update relation
“FAULT = ‘1009’ [Table with the name AppUser does not exist. Make sure the client class referenced in the API call has the same literal name as the table in Backendless console”
Unless you meant changing the table also in Backendless schema, is that what you mean?
You should work with Users table with BackendlessUser class - you don’t need another class for it. Please see this doc.
I shared by my test sample project (see in attachment), it shows how user relations work.
‘testUpdateUserWithRelatedUsers’ runs the add, retrieve and remove operations for 1:N relation ‘friends’. Could you try it and let me know how it goes for you.
TestUserRelations.zip (19.26MB)
Wow, it finally worked.
Even if it still complains with a fault at times.
Many thanks to you Vyacheslav. you have been a great help.
Thank you for providing a test sample as well as providing me with the right answer.
There’s a hint though I strongly advice to those who came up against the same issue.
It’s needed to retrieve a whole object before saving to it, specially it if belongs to a system table such as Users. Like in the following Reject Friend Request method I’m using now and working perfectly.
+ (void)rejectFriendRequest:(NSString *)userID {
id<IDataStore> dataStore = [backendless.persistenceService of:[BackendlessUser class]];
BackendlessUser *fullCurrentUser = [dataStore findID:backendless.userService.currentUser.objectId];
BackendlessUser *tempUser;
// Remove requesting user from current friend requests collection
NSMutableArray *friendRequests = [backendless.userService.currentUser getProperty:@"friendsRequest"];
for (BackendlessUser *user in friendRequests) {
if ([user.objectId isEqualToString:userID]) {
tempUser = user;
break;
}
}
[friendRequests removeObject:tempUser];
[fullCurrentUser setProperty:@"friendsRequest" object:friendRequests];
// Update current user
[[backendless.persistenceService of:[BackendlessUser class]] save:fullCurrentUser response:^(id response) {
} error:^(Fault *fault) {
NSLog(@"%@", fault.detail);
}];
}