How can we remove a particular relation object from backendless user table
Using code or management console?
Using ios code
I have written this code
BackendlessCollection *users = [[BackendlessCollection alloc] init];
users = backendless.userService.currentUser;
BackendlessUser *user= [[BackendlessUser alloc] init];//users;
user = users;
Responder *responder = [Responder responder:self
selResponseHandler:@selector(responseHandler1:)
selErrorHandler:@selector(errorHandler1:)];
[backendless.persistenceService remove:[user class] sid:strUnfollowObjId responder:responder];
WHERE strUnfollowObjId IS THE OBJECTID OF RELATION OBJECT
- Remove related object from the user object
Save user object
[[user class] removeObject:tblRetailer];
[backendless.persistenceService save:[user class]];
I used this code but it crashes with this error-
+[BackendlessUser removeObject:]: unrecognized selector sent to class 0x10bf70d00
hello ,
i used this code -
BackendlessCollection *users = [[BackendlessCollection alloc] init];
users = backendless.userService.currentUser;
BackendlessUser *user= [[BackendlessUser alloc] init];//users;
user = users;
[self removeContact:tblRetailer fromPhoneBook:user];
-(void)removeContact:(Retailer *)Ret fromPhoneBook:(BackendlessUser *)user
{
[user removeObject:Ret];
[backendless.persistenceService save:user];
}
But removeObject gives the error as No visible @interface for ‘BackendlessUser’ declares the selector remove object:
Aman,
Do you use code generated by Backendless console? If so, can you look into the generated code and make sure you’re calling the right method?
Regards,
Mark
Hi Mark,
I am not using code generated by Backendless console. Let me check the code generated by Backendless console and then get back to you.
Hi Mark,
I have tried the code generated by Backendless console, but this didn’t tell how to remove a relation from Users table. I tell you the structure in detail about a problem I am facing:
In Users table, we have added a column name ‘retailers’ which has one to many relation with table ‘Retailer’. We have marked the column ‘retailers’ as autoload. Please see the attach file of Users table properties.
We are trying to remove the relation of Retailer table with Users table using ‘retailers’ column in Users table by passing the Retailer Object. To do this, we have added a property ‘retailers’ (@property (nonatomic, strong) NSMutableArray *retailers;) in BackendlessUser.h class. See the attached file for more info.
When we get the result of logged in Users using query backendless.userService.currentUser. then this returns null value for ‘retailers’ column. See the attached file for more info.
If we get the null value for retailer column, we can’t remove the relation using “[user.retailers removeObject:retailerObject];”
BackendlessUser.txt (1.06kB)
UserDetails.txt (0.98kB)
Hi Aman,
We are investigating this problem and we will let you know ASAP
Regards,
Slava
Hi Aman,
If you are going not only to set user property but also “to remove” it - you should follow the next rules:
- From the console you must switch on “autoload” checkbox for this property of Users table:
http://support.backendless.com/public/attachments/f9c8afc15413ec4a759e1a5a431bc774.png</img> - To remove the value of this property you should use removeProperty method of BackendlessUser class and then save the user object, for example:
BackendlessUser *user = backendless.userService.currentUser;
[user removeProperty:@"task"];
NSLog(@"User has been changed: %@", user);
user = [[backendless.persistenceService of:BackendlessUser.class] save:user];
NSLog(@"User has been updated: %@", user);
You can use “removeProperties” method if you need to remove several properties:
[user removeProperties:@[@"task", @"tasks", @"location", @"locations"]];
Another way is to remove the related object:
BackendlessUser *user = backendless.userService.currentUser;
GeoPoint *location = [user getProperty:@"location"];
if ([location isKindOfClass:GeoPoint.class]) {
[backendless.geoService removePoint:location];
}
Task *task = [user getProperty:@"task"];
if ([task isKindOfClass:Task.class]) {
[[backendless.persistenceService of:Task.class] remove:task];
}
Regards,
Slava
Hi Slava,
Thanks for sending your response!
If you see my previous post, in which i already mentioned that we switched on the ‘autoload’ checkbox for retailers property. Please see the attached file.
But when we fetch the Users data based on logged in user(backendless.userService.currentUser), it returns the null value for retailers property. Refer to attach file for users data.
I believe if i use this query
[[backendless.persistenceService of:Task.class] remove:[user getProperty:@"task"]];
then it will remove all the relations not the specific relation? The property ‘retailers’ has one to many relation with Retailer table. It contains one or more Retailers object. We are looking to remove the specific related object not all.
UserDetails.txt (0.98kB)
I haven’t got any feedback from you on my last post. Could someone reply me on this?
We switched on the ‘autoload’ checkbox for retailers property. Please see the attached file.
But when we fetch the Users data based on logged in user(backendless.userService.currentUser), it returns the null value for retailers property. Refer to attach file for users data.
I believe if i use this query
[[backendless.persistenceService of:Task.class] remove:[user getProperty:@"task"]];
then it will remove all the relations not the specific relation? The property ‘retailers’ has one to many relation with Retailer table. It contains one or more Retailers object. We are looking to remove the specific related object not all.
Hi Aman,
This issue is fixed, you can get the fixed libraries from backendless github repo - https://github.com/Backendless/ios-SDK and try them (in your project you should change /lib/backendless & /lib/CommLibiOS from github repo)
Now to remove “retailers” user property you can use:
[backendless.userService.currentUser remove:@“retailers”];
If you need to remove several properties you should use the new method, for example:
[backendless.userService.currentUser removeProperties:@[@“task”, @“tasks”, @“location”, @“locations”]];
Regards,
Slava
Keep in the mind you can remove only loaded relation, so for user properties which you plan to remove the “autoload” checkbox of Users table must be switch on.
Another way - you can use the methods which load the objects with the relations, for example:
[[backendless.persistenceService of:BackendlessUser.class] load:user relations:@[@“task”, @“tasks”, @“location”, @“locations”]];
or
BackendlessUser *user = [[backendless.persistenceService of:BackendlessUser.class] findFirst:1];
and so on.
Hi Slava,
Thanks for sending the updated SDK, now we are getting retailers property objects using query backendless.userService.currentUser
We want to remove particular relation object of Retailers property not all the objects. If I use this query [backendless.userService.currentUser remove:@“retailers”]; then it will remove all the retailers’ related objects not the particular object. How could I remove the particular relation object of Retailers property from Users table?
Hi Slava,
Thanks for sending the updated SDK, now we are getting retailers property objects using query backendless.userService.currentUser
We want to remove particular relation object of Retailers property not all the objects. If I use this query [backendless.userService.currentUser remove:@“retailers”]; then it will remove all the retailers’ related objects not the particular object. How could I remove the particular relation object of Retailers property from Users table?
Hi Aman,
The Retailers property (as one-to-many relation) is NSMutableArray object, so if you would like to remove a particular relation object of Retailers property you can make the following actions:
- Get Retailers property from currentUser object:
NSMutableArray *retailers = [user getProperty:@"retailers"];
- Remove one object from array, for example
- by index:
[retailers removeObjectAtIndex:0];
- by objectId (or another Retailer object property value):
Retailer *removedRetailer = ...;
for (Retailer *retailer in retailers) {
if ([retailer.objectId isEqualToString:removedRetailer.objectId])
[retailers removeObject:retailer];
}
- Save the user object:
user = [[backendless.persistenceService of:BackendlessUser.class] save:user];
NSLog(@"aveUserWithRemovePropertiesSync - User has been updated: %@", user);
Regards,
Slava