iOS chat drop-down refresh chat record implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.