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

标签 ios objective-c xcode xmpp ejabberd

我是 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 委托(delegate)方法中添加了这段代码。这里在 userJid 中传递好友名,在 userJid1 中传递登录用户名。

关于ios - 如何从 iOS 中的 XMPP 服务器删除特定用户的聊天记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862400/

相关文章:

iOS:单个字符的 sizeWithFont 与字符串不同?

python - 什么时候调用 __lldb_init_module?

ios - 使用多个方案和推送通知配置配置文件

objective-c - objective-c : updating values for key for an NSMutableDictionary?

ios - 使用 slider 导航一系列图像

ios - 如果突出显示的单词是一行的第一个,textView 中的 NSBackgroundColorAttributeName 不尊重范围

ios - 仅适用于iOS7用户在App Store上提交现有应用的新版本。可能吗?

iOS 9 Parse (v 1.8.5) Facebook (v 4.6) FBSDKInternalUtility checkRegisteredCanOpenURLScheme 登录崩溃

ios - 调整 UITableView 背景 View 的框架?

iphone - NSString 比较抛出异常 SIGABT