0

I'm using Parse.com for my iOS application 8 ... In parse database I created a new class called "Relationships", what I'm trying to do is prefix the user of my app to send a friend request to another user. I'm not using PFRelation because I need that friend request is not automatic, but accepted by the user.

In short, the user sends the request richeista of friendship and this remains within the class "Relationship" with the status "Waiting" until the subscriber does not accept the request.

Now I'm able to do everything I can:

  • User pointer to register the two (receiver and forwarder's friend request)
  • Insert the request status "pending"

My problem is that if my user does not want more 'send the request can not' delete ..

I tried using the ["name of PFObject" deleteInBackground] but I can not delete anything ...

Can you help me figure out how to delete the newly created data from the database to parse?

#pragma mark ADD FRIENDS
-(void)addFriendUserButtonPressed:(UITableViewCell *)customCell  {


    NSIndexPath *indexPath = [self.tableViewFindUser indexPathForCell:customCell];

    PFObject *richiesta = [PFObject objectWithClassName:@"Relation"];

    if (!isFiltered) {

        PFUser *userFiltered = [self.userArray objectAtIndex:indexPath.row];

        if (![self Is_InAttesa:userFiltered]) {


            [richiesta setObject:userFiltered forKey:@"To_User"];
            [richiesta setObject:[PFUser currentUser] forKey:@"From_User"];
            [richiesta setObject:@"Pending" forKey:@"STATUS"];
            [richiesta saveInBackground];



        } else {


           //[richiesta removeObject:[PFUser currentUser] forKey:@"From_User"];
            //[richiesta setObject:userFiltered  forKey:@"STATUS"];
            //[richiesta saveInBackground];
        }



    }

    else {

        PFUser *userNotFiltered = [self.userFiltrati objectAtIndex:indexPath.row];


        [richiesta setObject:userNotFiltered forKey:@"To_User"];
        [richiesta setObject:[PFUser currentUser] forKey:@"From_User"];
        [richiesta setObject:@"Pending" forKey:@"STATUS"];
        [richiesta saveInBackground];

    }


}

This is the Boolean method that I created to recognize (through a query) if users are present in the list of pending friend requests

-(BOOL)Is_InAttesa:(PFUser *)user_inattesa {
    for (PFUser *userInAttesa in amiciInAttesaMutableArray) {
        if ([[[userInAttesa objectForKey:@"To_User"]objectId] isEqualToString:user_inattesa.objectId]) {
            return YES;
        }
    }
    return NO;
}

enter image description here

2
  • where is your deleting logic ? Also what user exactly wants to delete ? Commented Sep 18, 2014 at 9:22
  • The user should be able to delete the last friend request sent .. A few words I would like the user (who has requested friendship) will have the option to cancel the request you just submitted Commented Sep 18, 2014 at 9:37

2 Answers 2

1

Here is a method for deleting object from parse.

-(void)deleteButton {

  //Query or retrieving data from dB which you want to delete.
  PFQuery *query = [PFQuery queryWithClassName:@"YOUR_CLASS"];

  //This string in below case takes name from textfield that user wants to delete. For your case you could modify it as per your need.
  NSString *receiver_idStr =@"Id";
  NSString *sender_idStr =@"Id";

  // below two queries will work as like SELECT * FROM someTable WHERE senderId = 'id' AND receiverId = 'id'
  [query whereKey:@"request_sender_id" containsString:sender_idStr];
  [query whereKey:@"request_receiver_id" containsString:receiver_idStr];

  [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {  //Query fired in background to search in parse for this object with condition provided.

    if (!error) {
        NSLog(@"Successfully retrieved: %@", objects);

        //Now as you got object then you will type cast object from NSArray to PFObject and perform deleteInBackground method on them.

        //Also update that UI part ,i.e., remove the request object from UI.
    }
    else {
        NSLog(@"Error: %@", [error localizedDescription]);
    }
  }];
}

So this way to will be able to delete request object from parse. Also when user who has send the request cancel the request then also u search for that request object and do the same where else on user who receive request u could push notification to remove that request from it's UI.

In case user who receive request delete then it's simply find request object and delete, and update of UI for both sender and receiver.

Sign up to request clarification or add additional context in comments.

1 Comment

Walle Hello and thank you for your answer, in fact I was trying to work on this line of reasoning, the problem is that I do not have databrowser in the username field, but the links to the two pointer .. In this case, how could I do? I'll show you the databrowser class "Relations" I have attached the picture in my post at the top
0
-(void)addFriendUserButtonPressed:(UITableViewCell *)customCell  {


    NSIndexPath *indexPath = [self.tableViewFindUser indexPathForCell:customCell];
    PFObject *richiesta = [PFObject objectWithClassName:NPFriendClass];
    if (!isFiltered) {

        PFUser *userFiltered = [self.userArray objectAtIndex:indexPath.row];

        if (![self Is_InAttesa:userFiltered]) {




            [richiesta setObject:userFiltered forKey:NPFriend_AUser];
            [richiesta setObject:userFiltered.objectId forKey:@"OBJECT_USER_ID"];
            [richiesta setObject:userFiltered.username forKey:@"Username"];
            [richiesta setObject:[PFUser currentUser] forKey:NPFriend_DaUser];
            [richiesta setObject:@"Richiesta In Attesa" forKey:NPFriendRequestStatus];
            [richiesta saveInBackground];



        } else {


            PFQuery *query = [PFQuery queryWithClassName:NPFriendClass];
            [query whereKey:NPFriend_DaUser equalTo:[PFUser currentUser]];
            [query whereKey:NPFriendRequestStatus equalTo:@"Richiesta In Attesa"];
            [query whereKey:@"OBJECT_USER_ID" equalTo:userFiltered.objectId];
            [query includeKey:NPFriend_AUser];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

                if(!error) {
                    for (PFObject *object in objects) {
                        NSLog(@"Successfully retrieved: %@", object);
                        [object deleteInBackground];
                    }
                }

                else {
                    NSLog(@"Error: %@", [error localizedDescription]);
                }
            }];




                   }
    }

    else {

        PFUser *userNotFiltered = [self.userFiltrati objectAtIndex:indexPath.row];


        [richiesta setObject:userNotFiltered forKey:NPFriend_AUser];
        [richiesta setObject:[PFUser currentUser] forKey:NPFriend_DaUser];
        [richiesta setObject:@"Richiesta In Attesa" forKey:NPFriendRequestStatus];
        [richiesta saveInBackground];

    }

}

Hello Walle thanks again for your help you have been very kind and helpful ...

I fixed it this way and it seems to work ...

The only problem that remains is that it does not update the data immediately so the user can not figure out if you sent the request or not. The tableview is updated only if it does refresh the Tableview or change viewcontroller ..

I tried to redo do the query again as soon as the user sends a friend request but overlapping data and slows down the app ... How can I get the data refresh every minute without calling the query?

The idea of the button selected or not starch could be good? I'm trying but maybe something wrong because I can not get it to work

1 Comment

saveInBackground method makes a sync call so instead use [userObj saveInBackgroundWithBlock:^(BOOL succeed, NSError *error){ }];. So in block if succeed then show a alert to user for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.