OStack程序员社区-中国程序员成长平台

标题: ios - 如何在 iOS 的 openfire/xmpp 中获取所有在线用户的列表? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 01:44
标题: ios - 如何在 iOS 的 openfire/xmpp 中获取所有在线用户的列表?

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

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



Best Answer-推荐答案


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)controllerDidChangeContentNSFetchedResultsController *)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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4