I am also try to get all message from server but unable to get. So I retrieve message using in XMPPMessageArchivingCoreDataStorage and XMPPMessageArchiving
First declare the variables
private var xmppMessageStorage: XMPPMessageArchivingCoreDataStorage?
private var stream = XMPPStream()
private var xmppMessageArchiving: XMPPMessageArchiving?
When start connection
// for retrive old chat messages
xmppMessageStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
xmppMessageArchiving = XMPPMessageArchiving(messageArchivingStorage: xmppMessageStorage)
xmppMessageArchiving?.clientSideMessageArchivingOnly = true
xmppMessageArchiving?.activate(stream) //By this line all your messages are stored in CoreData
xmppMessageArchiving?.addDelegate(self, delegateQueue: DispatchQueue.main)
This method used get offline messages in CoreData for particular user
func getArchieveMessages(forUser user:String){
let fetchRequest = NSFetchRequest<NSFetchRequestResult>()
fetchRequest.predicate = NSPredicate(format: "bareJidStr = %@", user)
let context = xmppMessageStorage?.mainThreadManagedObjectContext
var messageEntity: NSEntityDescription? = nil
if let context = context {
messageEntity = NSEntityDescription.entity(forEntityName: "XMPPMessageArchiving_Message_CoreDataObject", in: context)
}
fetchRequest.entity = messageEntity
let sortDescriptor = NSSortDescriptor(key: "timestamp", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
var results: [Any]? = []
do {
results = try context?.fetch(fetchRequest)
self.modelController.delegate?.localHistoryMessages(results ?? [])
} catch let error {
debugPrint(error.localizedDescription)
}
debugPrint(results as Any)
}