我有 someclass 进行大型网络操作并且确实需要一些时间才能完成,因此我将其放入for
。我必须执行此过程' n& #39;所以在主线程中使用// inside main thread
for(i=0;i<n;i++)
{
new asynctask().execute(new someclass());
}
循环我称之为 asynctask n次。它会抛出任何错误,因为在完成for循环时有一个中断。?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.accessibilityIdentifier isEqualToString:@"minor"]) {
if (newLength == 0) {
minorMaxColumns = 5;
}
if (newLength == 2) {
if ([string integerValue] >= 6) {
minorMaxColumns = 4;
}
}
if (newLength == 3) {
if ([textField.text integerValue] >= 65) {
if ([string integerValue] >= 5) {
minorMaxColumns = 4;
}
}
}
if ([self isiBeaconMajorMinor:string] && (newLength <= minorMaxColumns)) {
return YES;
}
return NO;
}
-(BOOL)isiBeaconMajorMinor:(NSString*)inputString{
BOOL isValid = NO;
NS CharacterSet *alphaNumbersSet = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:inputString];
isValid = [alphaNumbersSet isSupersetOfSet:stringSet];
return isValid;
}
答案 0 :(得分:2)
不建议运行mutliple AsyncTask
,但如果它很少次,那么它将起作用,但所有异步任务都将串行运行而不是并行运行。但是,如果您希望异步任务并行运行,那么您可以调用它的executeOnExecutor(..)
方法,您必须将THREAD_POOL_EXECUTOR
作为参数传递。您可以在谷歌搜索,你可以找到很多链接。 Here就是您的帮助示例。
答案 1 :(得分:-1)
不要调用AsyncTask n次只需将你的for循环放在onPostExecute()中并执行n次任务
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
for(i=0;i<n;i++)
{
//do your operation here
}
}
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For
// example showing ProgessDialog
}
@Override
protected void onProgressUpdate(String... text) {
// Things to be done while execution of long running operation is in
// progress. For example updating ProgessDialog
}
}
}