1. Ideas
Recently in the development of a social-class app, you want to implement a similar kind of pull-down refresh chat record functionality.
There are generally two ways to achieve this:
1. Fetch all the data of an entity directly and then make the filter in memory, just read all the chat records and load 10 each time.
Cons: Wasted memory benefits: fast speed
2. Use predicate to read 10 data displays per Access database.
Disadvantage: Slow speed, there will be lag advantages: low memory
2. Implement
Because the chat record is very much, one reading is too wasteful of memory, it is decided to read 10 display from the database every time you pull down.
1-(Nsarray *) Querymessageresult2 {3Nsfetchrequest *request = [[Nsfetchrequest alloc] Initwithentityname:nsstringfromclass ([Historymessagelogclass])];4Nssortdescriptor *sortdescriptor = [[Nssortdescriptor alloc] Initwithkey:@" Time"Ascending:no];5 //add sorting and grouping rules to the request so that the chat record is in reverse chronological order, with the latest record in front6 [Request Setsortdescriptors:@[sortdescriptor]];7 8[Request Setfetchlimit:Ten];9 [Request setfetchoffset:self. Fetchoffset];Ten OneNserror *error; ANsarray *messagearray = [[(ID) [UIApplication sharedapplication].DelegateManagedobjectcontext] Executefetchrequest:request error:&ERROR]; - if([Messagearray Count]) { - //to update the cursor every time theSelf. Fetchoffset + =[Messagearray Count]; - } - returnMessagearray; - +}
So every time can read 10 records out, this implementation is relatively easy, do complex can be based on time to read a certain number of bars.
3. Code
Chat function is almost done, but there are some small problems, the following is
If you have interested friends can download the code to see, and then communicate together.
GitHub Address: Https://github.com/stevenxiaoyang/Chat.git
iOS chat drop-down refresh chat record implementation