Making stringWithContentsOfURL asynchronous - Is it safe?

后端 未结 3 2024
灰色年华
灰色年华 2020-12-18 05:17

I attempted to make -[NSString stringWithContentsOfURL:encoding:error:] asynchronous, by running it a-synchronically from a background thread:

__block NSStri         


        
3条回答
  •  感动是毒
    2020-12-18 05:49

    -(void)loadappdetails:(NSString*)appid {
        NSString* searchurl = [@"https://itunes.apple.com/lookup?id=" stringByAppendingString:appid];
    
        [self performSelectorInBackground:@selector(asyncload:) withObject:searchurl];
    
    }
    -(void)asyncload:(NSString*)searchurl {
        NSURL* url = [NSURL URLWithString:searchurl];
        NSError* error = nil;
        NSString* str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
        if (error != nil) {
            NSLog(@"Error: %@", error);
        }
        NSLog(@"str: %@", str);
    }
    

提交回复
热议问题