繁体   English   中英

如何从iOS中的XMPP服务器删除特定用户的聊天?

[英]How to delete particular user's chat from XMPP server in iOS?

我是iPhone应用程序开发的新手。 我正在使用XMPP和Ejabberd在iOS上的聊天应用程序上工作。 但是我无法删除特定用户的聊天记录(不能删除一条消息)。

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self DeleteMessageChat:indexPath];
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [messages removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
    }
}

-(void)DeleteMessageChat :(NSIndexPath *)indexpath
{
    self.cls=[self.arrProfile objectAtIndex:indexpath.row];
    NSString *userJid = [NSString stringWithFormat:@"%@%@",self.cls.UserId,Xmppserver];
    XMPPMessageArchivingCoreDataStorage *storage = appDelegate.xmppMessageArchivingStorage;
    NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
                                                         inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:entityDescription];
    NSString *predicateFrmt = @"bareJidStr == %@";
    NSError *error;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFrmt, userJid];
    request.predicate = predicate;
    NSArray *messages_new = [moc executeFetchRequest:request error:&error];

    NSManagedObject *obj=[messages_new objectAtIndex:indexpath.row];
    [moc deleteObject:obj];

    error = nil;
    if (![moc save:&error])
    {
        NSLog(@"Error deleting movie, %@", [error userInfo]);
    }
}

请帮我解决这个问题。

我在代码中做到了这一点。 尝试根据您的数组进行编辑。

NSString *userJid = [NSString stringWithFormat:@"%@%@",cell.frndName.text,DomainName];
        NSString *userJid1= [NSString stringWithFormat:@"%@%@",[[NSUserDefaults standardUserDefaults]valueForKey:@"name"],DomainName];

        NSFetchRequest *messagesCoreD = [[NSFetchRequest alloc] init];
        NSManagedObjectContext *context=[[self appDelegate].xmppMessageArchivingCoreDataStorage mainThreadManagedObjectContext];
        NSEntityDescription *messageEntity = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Contact_CoreDataObject" inManagedObjectContext:context];
          [messagesCoreD setEntity:messageEntity];
        [messagesCoreD setIncludesPropertyValues:NO]; //only fetch the managedObjectID
        NSString *predicateFrmt = @"bareJidStr == %@";
        NSString *predicateFrmt1 = @"streamBareJidStr == %@";
        NSPredicate *predicateName = [NSPredicate predicateWithFormat:predicateFrmt,userJid];
        NSPredicate *predicateSSID = [NSPredicate predicateWithFormat:predicateFrmt1,userJid1];

        NSArray *subPredicates = [NSArray arrayWithObjects:predicateName, predicateSSID, nil];

        NSPredicate *orPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];

        messagesCoreD.predicate = orPredicate;
        NSError * error = nil;
        NSArray * messages = [context executeFetchRequest:messagesCoreD error:&error];
        //error handling goes here
         [tableView reloadData];
        for (NSManagedObject * message in messages)
        {
            [context deleteObject:message];
            [tableView reloadData];
        }
        NSEntityDescription *messageEntity1 = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject" inManagedObjectContext:context];
        [messagesCoreD setEntity:messageEntity1];
        [messagesCoreD setIncludesPropertyValues:NO]; //only fetch the managedObjectID

        messagesCoreD.predicate = orPredicate;
        NSArray * messages1 = [context executeFetchRequest:messagesCoreD error:&error];
        //error handling goes here
        [tableView reloadData];
        for (NSManagedObject * message in messages1)
        {
            [context deleteObject:message];
            [tableView reloadData];
        }

        NSError *saveError = nil;
        [context save:&saveError];

我在commitEditingStyle委托方法中添加了此代码。 在这里,您在userJid中传递朋友名称,并在userJid1中登录用户名称。

暂无
暂无

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

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