(NSFetchedResultsController *)fetchedResultsController {
// 推荐写法,减少嵌套的层次
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
// 先确定需要用到哪个实体
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPMessageArchiving_Message_CoreDataObject"];
// 排序
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES];
request.sortDescriptors = @[sort];
// 只读取当前用户的消息
NSLog(@"streamBareJidStr============%@",[SXXMPPTools sharedXMPPTools].xmppStream.myJID);
*
// 从自己写的工具类里的属性中得到上下文
NSManagedObjectContext *ctx = [SXXMPPTools sharedXMPPTools].xmppMessageArchivingCoreDataStorage.mainThreadManagedObjectContext;
// 实例化,里面要填上上面的各种参数
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:ctx sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
[self.tableview reloadData];
}
加了这句查询条件 request.predicate = [NSPredicate predicateWithFormat:@"streamBareJidStr=%@",[SXXMPPTools sharedXMPPTools].xmppStream.myJID];我加了这句查询条件数据库内容改变就不会回调了
这个就是读取XMPP聊天消息的数据 因为要切换账号登录 我不想把之前账号的历史消息也查出来所有要加这个predicate