ios - 如何在 iOS 的 openfire/xmpp 中获取所有在线用户的列表?

标签 ios xmpp openfire xmppframework

我正在获取在线/离线用户。这样如果我需要检查我必须每次发送请求以获得存在

我如何检查多个用户或仅在线用户?

最佳答案

I have implement NSFetchedResultsControllerDelegate. i am getting list of Online user in "SectionNum"=0. whenever user goes offline/online controller's delegate method called.accordingly update tableView.

//NSFetchedResultsController *fetchedResultsController;//实例变量

in viewWillAppear

 //xmpp user array
    self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy];

    for (int i=0; i<[[self xmppUserArray] count]; i++) {

        if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) {
            //this is user is online
            [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]];

        }
    }


//also implement method
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    //remove previous data or clear array

    [[self xmppUserArray] removeAllObjects];
    [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] removeAllObjects];


    //get data from core data
    self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy];


    for (int i=0; i<[[self xmppUserArray] count]; i++) {

        if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) {
            //this is user is online
            [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]];

        }
    }


    [[self msgTableView] reloadData];

}



-(NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController == nil)
    {
        NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
                                                  inManagedObjectContext:moc];

        NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
        NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

        NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
        //NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

        //NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userJID"];
        //NSLog(@"My JID ====>%@",myJID);

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subscription=='both'"];//take care about subscription


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:entity];
        [fetchRequest setPredicate:predicate];
        [fetchRequest setSortDescriptors:sortDescriptors];
        [fetchRequest setFetchBatchSize:20];

        fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                       managedObjectContext:moc
                                                                         sectionNameKeyPath:@"sectionNum"
                                                                                  cacheName:nil];
        [fetchedResultsController setDelegate:self];


        NSError *error = nil;
        if (![fetchedResultsController performFetch:&error])
        {
            DDLogError(@"Error performing fetch: %@", error);
        }

    }

    return fetchedResultsController;
}

关于ios - 如何在 iOS 的 openfire/xmpp 中获取所有在线用户的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25803196/

相关文章:

java - OpenFire 服务器中用户的 JID 应该是什么?

android - Smack 以 XML 格式接收消息,我可以将它们更改为 JSON 吗?

ios - 核心数据一对一关系更新

ios - TabBarController 中的 UIImagePickerController

ios - 使用 CSRF token 从 iOS POST 到 Django 服务器

c++ - 如何在不创建服务器的情况下创建具有联系人和实时通信的多人游戏?

javascript - Twilio 测试电话号码无法使用

XMPP BOSH 与 COMET

android - GCM XMPP CCS 与 MQTT

java - 我们可以延迟响应 XMPP 状态订阅吗?