简体   繁体   English

从Parse.com(iOS App)的数据库中删除项目

[英]Delete item from the database of Parse.com (iOS App)

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. 我正在为我的iOS应用程序8使用Parse.com ...在解析数据库中,我创建了一个名为“ Relationships”的新类,我想做的是在我的应用程序的用户前面添加一个向另一个用户发送好友请求的功能。 I'm not using PFRelation because I need that friend request is not automatic, but accepted by the user. 我没有使用PFRelation,因为我需要朋友请求不是自动的,而是被用户接受的。

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. 简而言之,用户发送友谊请求richeista,并且该请求richeista保持在状态为“等待”的“关系”类中,直到订户不接受该请求。

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 ... 我尝试使用[“ PFObject的名称” deleteInBackground],但无法删除任何内容...

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;
}

在此处输入图片说明

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. 如果接收请求的用户删除,则只需找到请求对象并删除,以及更新发送者和接收者的UI。

-(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 ... 您好Walle再次感谢您的帮助,您一直非常友好和乐于助人。

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 .. 仅在刷新表视图或更改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 我正在尝试,但是可能出了点问题,因为我无法正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM