IOS chat pull-down refresh the implementation of chat records, ios chat records

Source: Internet
Author: User

IOS chat pull-down refresh the implementation of chat records, ios chat records
1. Ideas

Recently, I was developing a social app to implement a similar pull-down refresh chat record function.

There are two implementation methods:

1. directly fetch all the data of an entity and then filter the data in the memory. This means to read all the chat records first and then load 10 records each time.

Disadvantage: A waste of memory advantages: fast

2. Use predicate to read 10 pieces of data each time you access the database.

Disadvantage: slow speed, choppy performance; advantage: less memory

 

2. Implementation

Because there are a lot of chat records, it is a waste of memory to read at a time, and it is decided to read 10 display records from the database at each pull-down.

1-(NSArray *) queryMessageResult 2 {3 NSFetchRequest * request = [[NSFetchRequest alloc] initWithEntityName: NSStringFromClass ([HistoryMessageLog class])]; 4 NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @ "time" ascending: NO]; 5 // Add the sorting and grouping rules to the request, in this way, the chat record is displayed in the first 6 [request setSortDescriptors: @ [sortDescriptor]; 7 8 [request setFetchLimit: 10]; 9 [request setFetchOffset: self. fetchOffset]; 10 11 NSError * error; 12 NSArray * messageArray = [[(id) [UIApplication sharedApplication]. delegate managedObjectContext] executeFetchRequest: request error: & error]; 13 if ([messageArray count]) {14 // update the cursor 15 self each time. fetchOffset + = [messageArray count]; 16} 17 return messageArray; 18 19}

In this way, 10 records can be read each time, which is easy to implement and complicated to read a certain number of records by time.

 

3. Code

The chat function is almost done, but there are still some minor problems. below is

 

If you are interested, you can download the code and try again.

Github address: https://github.com/stevenxiaoyang/Chat.git

Related Article

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.