if let aView: CBLView = self.database?.viewNamed("cmview"){ aView.setMapBlock({ (doc, emit) in if let messageDate = doc["messageDate"]{ emit(messageDate, doc) } }, version: "1") }
if let aView = self.database?.existingViewNamed("cmview"){ chatMessagesQuery = aView.createQuery().asLive() if let query = chatMessagesQuery{ let loggedInUserDocumentID = <a UUID string> let otherUserDocumentID = <a UUID string> let predicate = NSPredicate(format: "(value.fromId = %@ && value.toId = %@) || (value.fromId = %@ && value.toId = %@)", loggedInUserDocumentID,otherUserDocumentID,otherUserDocumentID,loggedInUserDocumentID) query.postFilter = predicate query.descending = true query.limit = 30 //The query is fetching documents in batches of 30 each do{ _ = try query.run() }catch{ print("Encountered error: \(error) while fetching chat messages.") } } }
emit([doc.messageDate,doc.fromId,doc.toId], doc)
On Jul 20, 2017, at 7:37 AM, parvez....@decurtis.com wrote:Shouldn't the view's emit block to include the fromId, toId along with messageDate as key as shown below:
emit([doc.messageDate,doc.fromId,doc.toId], doc)
On Jul 27, 2017, at 7:39 AM, parvez....@decurtis.com wrote:Can you elaborate a little more on the use of min / max function inside the emit block ?
Since we are specifying the messagerDate only in value field of emit section then how we can get the complete chat message corresponding to this date when query is run?