iOS data optimization: processing HTML strings

iOS data optimization: processing HTML strings

The problem encountered in the recent project is that because the data returned by the background is an HTML string, the HTML string is converted into a rich text string according to the conventional processing method. It turns out that the tableview will be very stuck and cause thread blocking and unable to respond to events.

[[212539]]

  1. In the set method of the cell model, this is how it was done at the beginning~~~~~ Very stuck
  2. -(void)setModel:(XAPublicWelfareModel *)model{
  3. //This is the time-consuming operation code
  4. NSAttributedString * attrStr = [[NSAttributedString alloc]initWithData:[model.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
  5. self.introLabel.attributedText = attrStr;
  6. }

Solution 1

The first thing I thought of was to put the time-consuming operation in the child thread

  1. //1. Get a global serial queue
  2. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  3. //2. Add the task to the queue for execution
  4. dispatch_async(queue, ^{
  5.           
  6. NSAttributedString * attrStr = [[NSAttributedString alloc]initWithData:[model.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
  7.           
  8. dispatch_async(dispatch_get_main_queue(), ^{
  9.               
  10. self.introLabel.attributedText = attrStr;
  11.               
  12. });
  13.           
  14. });

Although it solves the problems of screen freeze and thread blocking, it does not solve the fundamental problem. Data processing is still very slow. It is not recommended to use

Solution 2

Because it is a cell display, you only need to display text information, so filter out HTML tags and instantly solve all problems. So when displaying data in a list, be careful when converting HTML to NSAttributedString.

  1. -(void)setModel:(XAPublicWelfareModel *)model{
  2. //Call the method to remove HTML tags and assign values ​​directly.
  3. self.introLabel.text = [self filterHTML:model.content];
  4. }
  5. //How to remove the label
  6. -(NSString *)filterHTML:(NSString *)html
  7. {
  8. NSScanner * scanner = [NSScanner scannerWithString:html];
  9. NSString * text = nil;
  10. while([scanner isAtEnd]== NO )
  11. {
  12. [scanner scanUpToString:@ "<" intoString:nil];
  13. [scanner scanUpToString:@ ">" intoString:&text];
  14. html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@ "%@>" ,text] withString:@ "" ];
  15. //Remove spaces
  16. html = [html stringByReplacingOccurrencesOfString:@ " " withString:@ "" ];
  17. }
  18. return html;
  19. }

Here is a brief introduction to NSScanner

NSScanner is a class used to scan the specified characters in a string and translate them into the string or number we need. The core is the movement of the position, that is, the movement of scanLocation.

In the above method, we first specify the object to be scanned html (NSString) NSString * text It is very important to store the string we want to scan in text

The scanned string is the string before >. The scanUpToString method means to stop scanLocation before > and pass the previous string to text.

Let's look back at our method of removing HTML tags. The whole process is carried out during the scanning process. When NSScanner executes the scanUpToString method, once it scans the required string, such as "<" in the example, its scanLocation will become the initial position of the HTML. Therefore, after executing a complete scan, the HTML tag should be replaced with an empty string before the next scan. That is to say, the tag characters of the HTML string will become fewer and fewer in the while, and the initial position of each scan will remain relatively unchanged and stay at the end of the previous scan, that is, in front of the "<" tag.

<<:  A brief history of the imported term “404 error”: “The page you are looking for does not exist”

>>:  Android interview 17 knowledge points summary

Recommend

Zuckerberg gave a 20-minute speech in Chinese. What did he say?

[[121571]] Still wearing his signature grey short...

What exactly are user insights? How to do it?

I often hear people compare marketing to "te...

How to boost your immunity? Eat like a rainbow!

Written by: Li Caihong, Chief Nutrition Technicia...

Bizarre: A review of the top 10 retractions in the life sciences in 2021

Since the outbreak of the COVID-19 pandemic, jour...

How to make App sharing stand out like Keep?

When the product content and quality have reached...

Staying up late because of the epidemic? You will regret it someday!

With the frequent outbreaks of the epidemic, have...

Kuaishou Search for Hidden Traffic Opportunities

When users' content consumption needs extend ...

View-Master 2.0 released: the best performing Google VR Cardboard product

Last year, Mattel completely redesigned the View-...

Review! Alipay's promotional tactics of sending 1 billion red envelopes

After the amazing copywriting of Alipay red envel...

The ultimate secret to brand success: perseverance!

In the past few days, there are several names tha...