(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