自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS數(shù)據(jù)優(yōu)化之處理HTML字符串

移動(dòng)開發(fā)
最近項(xiàng)目遇到的問題,因?yàn)楹笈_(tái)返回的數(shù)據(jù)是HTML字符串,所以就按照常規(guī)處理方式把HTML字符串轉(zhuǎn)換成富文本的字符串來處理,事實(shí)證明,tableview會(huì)非???,并且造成線程阻塞,無法響應(yīng)事件

最近項(xiàng)目遇到的問題,因?yàn)楹笈_(tái)返回的數(shù)據(jù)是HTML字符串,所以就按照常規(guī)處理方式把HTML字符串轉(zhuǎn)換成富文本的字符串來處理,事實(shí)證明,tableview會(huì)非??ǎ⑶以斐删€程阻塞,無法響應(yīng)事件

[[212539]]

 

  1. 在cell的model的set方法中剛開始是這樣操作的~~~~~非???nbsp;
  2. -(void)setModel:(XAPublicWelfareModel *)model{ 
  3. //這就是耗時(shí)操作的代碼 
  4. NSAttributedString * attrStr = [[NSAttributedString alloc]initWithData:[model.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil]; 
  5.  self.introLabel.attributedText = attrStr; 

解決方案1

首先我想到的是把耗時(shí)操作放在子線程來操作

 

  1. //1.獲取一個(gè)全局串行隊(duì)列 
  2.     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
  3. //2.把任務(wù)添加到隊(duì)列中執(zhí)行 
  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.     }); 

雖然解決了,卡屏,線程阻塞的問題,但是并沒有解決根本問題,數(shù)據(jù)處理還是很慢,不建議使用

解決方案2

因?yàn)槭莄ell展示,所以只需要展示文本信息就行,那就過濾掉HTML標(biāo)簽,瞬間解決所有問題。所以在列表展示數(shù)據(jù)的時(shí)候HTML轉(zhuǎn)換NSAttributedString一定要慎用

 

  1. -(void)setModel:(XAPublicWelfareModel *)model{ 
  2. //調(diào)用去除HTML標(biāo)簽的方法,直接賦值。 
  3. self.introLabel.text = [self filterHTML:model.content]; 
  4. //去除標(biāo)簽的方法 
  5. -(NSString *)filterHTML:(NSString *)html 
  6.     NSScanner * scanner = [NSScanner scannerWithString:html]; 
  7.     NSString * text = nil; 
  8.     while([scanner isAtEnd]==NO
  9.     { 
  10.         [scanner scanUpToString:@"<" intoString:nil]; 
  11.         [scanner scanUpToString:@">" intoString:&text]; 
  12.         html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""]; 
  13.         //去除空格   
  14.         html = [html stringByReplacingOccurrencesOfString:@" " withString:@""]; 
  15.     } 
  16.     return html; 

下面簡(jiǎn)單介紹一下NSScanner

NSScanner是一個(gè)類,用于在字符串中掃描指定的字符,翻譯成我們需要的字符串或者數(shù)字,核心就是位置的移動(dòng) 即scanLocation的移動(dòng)變化

在上面的方法中首先指明了要掃描的對(duì)象 html(NSString) NSString * text 很重要 把我們要掃描出來的字符串存到text里面

而這個(gè)掃描到的字符串就是>之前的字符串 scanUpToString這個(gè)方法的意思是將scanLocation停留在>之前 并把之前的字符串傳給text。

回頭來看看我們?nèi)コ齢tml標(biāo)簽的方法 整個(gè)過程都是在掃描過程中進(jìn)行的NSScanner在執(zhí)行scanUpToString這個(gè)方法時(shí)一旦掃描到需要的字符串比如例子中的“<”,其scanLocation就會(huì)變?yōu)閔tml的初始位置。所以,要在執(zhí)行完一次完整的掃描后 把html標(biāo)簽用空字符串替換掉,在進(jìn)行下一次掃描,也就是說再while中 html字符串的標(biāo)簽字符會(huì)越來越少,而每次掃描的初始位置相對(duì)沒有變化都停留在上一次掃描結(jié)束的位置,即"<"標(biāo)簽的前面。

責(zé)任編輯:未麗燕 來源: 你太善良_
相關(guān)推薦

2010-11-26 09:51:54

MySQL字符串

2010-07-14 16:35:52

Perl字符串處理函數(shù)

2010-08-04 11:23:15

Flex字符串

2013-05-02 11:13:05

C++遇到iOS應(yīng)用開字符串處理

2009-08-07 15:58:54

C#字符串插入html

2010-06-04 14:59:06

MySQL數(shù)據(jù)庫

2010-07-19 15:07:46

Perl字符串處理函數(shù)

2010-10-09 11:54:46

MySQL字符串

2023-08-14 16:30:46

2024-01-03 08:20:05

Java字符串性能

2019-12-17 15:49:44

Java語言字符串

2009-11-26 16:26:32

PHP字符串mbstr

2016-12-30 13:32:24

字符串算法代碼

2010-11-26 11:20:31

MySQL字符串處理函

2019-08-12 14:25:09

編程算法PythonJavaScript

2020-05-12 08:53:15

JavaScript字符串處理庫

2021-08-26 11:41:50

字符串String.jsVoca

2023-10-18 07:55:41

Python字符串

2024-09-06 17:32:55

字符串Python

2009-11-17 10:55:02

PHP字符串處理函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)