Reputation: 649
I'm now using a function which returns NSString after searching html, The code is
- (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";
}
which works fine but when I check the running time each of the function in 'searchCoupang' such like
NSString *testString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]
encoding:NSUTF8StringEncoding
error:nil];
and
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];
}
works few millisecond but when I check the time right after the function which call 'searchCoupang' time delayed few seconds
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;
}
Where does this delay come from?
Thanks,
Upvotes: 0
Views: 327
Reputation: 2227
are you sure it's not - stringWithContentsOfURL
that's causing the delay? it's a synchronous function so the application has to wait while it connects to the URL, downloads the data etc. for a production environment, you'd really want to move to an async process. you can do this via threads or grand central dispatch. personally i've found GCD to be an easy way to do this kind of thing.
if it's not the downloading that's causing it, i can't see what would be.. unless you're talking about massively long strings.. but then that would imply a long download time.
Upvotes: 1