gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:20:44 24 4
gpt4 key购买 nike

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

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

最佳答案

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com