Reputation: 211
So , I've made a class called Posts
in Parse.com.. This is the image of the class Posts
here you can see in objectId
column there are objectIds of all the Posts
and in the column likes
I'm saving the ObjectIds of users who is liking the post.. so basically if a user tap on unlike button the current user's Id should be deleted . this is my current code:
var query:PFQuery = PFQuery(className: "Posts")
query.whereKey("objectId", equalTo: postData.objectId!)
query.whereKey("likes", equalTo: PFUser.currentUser()!.objectId!)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
object.deleteInBackground()
}
}
})
but it deletes the whole row not the user id of current user..
Upvotes: 1
Views: 2198
Reputation: 1
I have used your method to remove a users objectID from another users friends list and instead add it to the users blocked friends list, both are in an array on parse. Thanks
let cell = tableView.cellForRow(at: indexPath) as! cell
let theUsersID = cell.theObjectID.text!
PFUser.current()?.addUniqueObjects(from: [theUsersID], forKey: "friendList")
PFUser.current()?.saveInBackground()
PFUser.current()?.removeObjects(in: [theUsersID], forKey: "blockFriend")
PFUser.current()?.saveInBackground()
self.usernames.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
Upvotes: 0
Reputation: 1239
I believe you are storing userID in your likes array as strings instead of pointers.
In your code, the key "likes" is an array type, but you want to query an objectId which is a string type, clearly parse won't find any column matches this query.
so in your case, you want to remove one of the strings in your likes array(objective c code)
PFQuery *query = [PFQuery queryWithClassName@"Posts"];
[query whereKey:@"objectId" equalTo:postData.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *object, NSError *error)
{
if (! error)
{
//get the post object, index 0 because each post has 1 unique ID
PFObject *thisObject = [object objectAtIndex:0];
//get the likes array
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:thisObject[@"likes"]];
[array removeObject:/* your user id*/];
//save the new array
[thisObject setObject:array forKey:@"likes"];
[thisObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
if (succeeded)
{
//success
}
}];
}
}];
Upvotes: 0
Reputation: 9912
You can use removeObject:forKey:
to only remove this single object from the array. After doing this, call saveInBackground
to save the changes back to the server.
See also: https://www.parse.com/docs/ios/guide#objects-arrays
Upvotes: 2