【问题标题】:How to delete particular user's chat from XMPP server in iOS?如何从 iOS 的 XMPP 服务器中删除特定用户的聊天?
【发布时间】:2015-12-28 00:30:48
【问题描述】:

我是 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]);
    }
}

请帮我解决这个问题。

【问题讨论】:

    标签: ios objective-c xcode xmpp ejabberd


    【解决方案1】:

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

    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 中登录用户名。

    【讨论】:

    • Tysm。抱歉,因为我没有足够的声誉,所以我无法投票。
    • 没问题,它有帮助。够了 :) 度过了愉快的一天
    猜你喜欢
    • 2019-09-24
    • 2015-10-26
    • 2015-04-03
    • 2018-06-05
    • 2015-11-05
    • 2012-06-14
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多