2011-12-30 135 views
0

我現在使用它搜索HTML後返回的NSString功能, 的代碼是Objective-C的功能延遲

- (IBAction) analysys:(id)sender { 

    comparisonOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch; 

    NSString *coupangURL = @"http://www.coupang.com/alldeal.pang";  

    NSMutableArray * title = [[NSMutableArray alloc] init]; 
    [title addObject:(NSString*)@"box"]; 

    for (NSString * e in title) { 

     NSString * addr = [self searchCoupang:coupangURL targetString:e]; 
     self.myTextView.text = addr; 
    } 
} 

- (NSString*) searchCoupang:(NSString *) url targetString:(NSString*) tString{ 

    NSString *testString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] 
                encoding:NSUTF8StringEncoding 
                 error:nil]; 

    NSString * targetURL; 

    if (testString != NULL){ 
     // Find the string 

     NSRange rangeOfTargetString = [testString rangeOfString:tString options:comparisonOptions]; 

     t = [NSDate timeIntervalSinceReferenceDate]; 

     if(rangeOfTargetString.location != NSNotFound) { 

      // Adjust range to take the line takes URL 
      NSRange cutRange = {rangeOfTargetString.location - cutStringValueToGetTheAddress, cutStringValueToGetTheAddress}; 

      // Line which takes URL 
      NSString * lineContainsURL = [testString substringWithRange:cutRange]; 

      NSRange rangeStartOfURL = [lineContainsURL rangeOfString:@"href"]; 
      NSRange rangeEndOfURL = [lineContainsURL rangeOfString:@"onclick"]; 

      NSRange targetRangeOfURL = {rangeStartOfURL.location + 6 ,((rangeEndOfURL.location - 2) - (rangeStartOfURL.location + 6)) }; 

      targetURL = [lineContainsURL substringWithRange:targetRangeOfURL]; 

     } 

     return targetURL; 
    } 
    else 
     return @"Reading url error"; 
} 

工作正常,但是當我檢查的運行時間每個「searchCoupang功能「諸如此類

NSString *testString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] 
                encoding:NSUTF8StringEncoding 
                 error:nil]; 

if (testString != NULL){ 
    // Find the string 

    NSRange rangeOfTargetString = [testString rangeOfString:tString options:comparisonOptions]; 

    t = [NSDate timeIntervalSinceReferenceDate]; 

    if(rangeOfTargetString.location != NSNotFound) { 

     // Adjust range to take the line takes URL 
     NSRange cutRange = {rangeOfTargetString.location - cutStringValueToGetTheAddress, cutStringValueToGetTheAddress}; 

     // Line which takes URL 
     NSString * lineContainsURL = [testString substringWithRange:cutRange]; 

     NSRange rangeStartOfURL = [lineContainsURL rangeOfString:@"href"]; 
     NSRange rangeEndOfURL = [lineContainsURL rangeOfString:@"onclick"]; 

     NSRange targetRangeOfURL = {rangeStartOfURL.location + 6 ,((rangeEndOfURL.location - 2) - (rangeStartOfURL.location + 6)) }; 

     targetURL = [lineContainsURL substringWithRange:targetRangeOfURL]; 

    } 

工作幾毫秒,但是當我查看了「搜索延時」時間延遲幾秒鐘後的功能。

for (NSString * e in title) { 
    // start to check the time 
    NSString * addr = [self searchCoupang:coupangURL targetString:e]; 
    // End checking time (takes few seconds) 
    self.myTextView.text = addr; 
} 

此延遲來自哪裏?

感謝,

回答

1

你確定它不是- stringWithContentsOfURL這是造成延誤?它是一個同步功能,因此應用程序必須等待連接到URL,爲生產環境下載數據等,您真的想要移動到異步進程。你可以通過線程或大中央調度來完成。我個人認爲GCD是做這種事情的一種簡單方法。

如果它不是下載導致它,我不能看到會是什麼..除非你在談論大量的長字符串..但那麼這意味着一個漫長的下載時間。

+0

+1。我不認爲OP知道'-stringWithContentsOfURL:'代表他執行同步的'NSURLConnection'。 – 2011-12-31 01:17:59

+0

實際加載的html文件的大小很大,但是當我用小尺寸的html文件(帶有URL)檢查它時,它的時間幾乎是相同的,所以我認爲它不是downlonding數據大小的問題。但連接到URL將是問題。而當我重複3〜4次時,它會變快。會不會因爲它保存了文件? – 2011-12-31 05:14:23

+0

是的,它可能確實在緩存文件的過程中的某個時刻。 – 2011-12-31 05:21:10