iOS WritEtourl延迟或屏幕冻结,如何在情况下解决线程



我有一个复杂的操作,然后在操作完成时,

它将保存到NSDATA写入文件以预览的文件。

我遇到问题,当我单击按钮时,

按钮操作将开始显示MBProgress的事物和异步在背景中运行。

当文件写入成功时,它将用于preparforsegue方法将值传递给destinationviewController。

我尝试添加线程,但是我发现我的屏幕总是冻结的,或者无法编写文件成功以保持此屏幕显示警报(我认为它的操作还不完整,所以请获得bool是否定的)。

如何在这种情况下编写解决方案以显示MBProgress等待操作完成,然后导航到Next ViewController?

非常感谢。

我的代码下面:

 - (IBAction)fileBtnAction:(UIButton *)sender{
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    [self doComplexOperateAction];
    dispatch_async(dispatch_get_main_queue(), ^{
        fileHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        fileHud.label.text = @"file operating....";
        fileHud.userInteractionEnabled = NO;
        });
    });
 }
 - (void) doComplexOperateAction{
       ....... do something.......
      NSError *error;
writeFileSuccess = [resultFilie.fileContent writeToURL:previewFileUrl
                                                         options:NSDataWritingFileProtectionComplete
                                                             error:&error];
 }
 -(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
 if( [identifier isEqualToString:@"fileSuccessSegue"] ){        
         if( writeFileSuccess == YES ){
            fileHud.hidden = YES;
            fileHud = nil;
             return YES;
         }else{
             dispatch_async(dispatch_get_main_queue(), ^{
          msgAlertController.message = @"can't write file success";
          [self presentViewController:msgAlertController animated:YES completion:nil];
            fileHud.hidden = YES;
            fileHud = nil;
        });
        return NO;
    }
}
   return NO;
 }
 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
 if( [[segue identifier] isEqualToString:@"fileSuccessSegue"] ){
     .........
     NSURL *fileURL = [NSURL fileURLWithPath:fileTmpPathString];
     fileSuccessViewController *pfVC = [segue destinationViewController];
     pfVC.filePathURL = fileURL;
 }
 }

我认为这将是这样的:

@property (noatomic) BOOL uploading;
- (IBAction)fileBtnAction:(UIButton *)sender{
    fileHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    fileHud.label.text = @"file operating....";
    fileHud.userInteractionEnabled = NO;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        [self doComplexOperateAction];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self performSegueWithIdentifier:@"fileSuccessSegue" sender:nil];
        });
    });
}
- (void) doComplexOperateAction{
    self.uploading = YES;
    ....... do something.......
    NSError *error;
    writeFileSuccess = [resultFilie.fileContent writeToURL:previewFileUrl
                                                     options:NSDataWritingFileProtectionComplete
                                                         error:&error];
    self.uploading = NO;
}
-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if( [identifier isEqualToString:@"fileSuccessSegue"] ){        
        if( writeFileSuccess == YES ){
            fileHud.hidden = YES;
            fileHud = nil;
            return YES;
        }else{
            msgAlertController.message = @"can't write file success";
            [self presentViewController:msgAlertController animated:YES completion:nil];
            fileHud.hidden = YES;
            fileHud = nil;
        });
        return NO;
        }
    }
    return !self.uploading;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if( [[segue identifier] isEqualToString:@"fileSuccessSegue"] ){
        .........
        NSURL *fileURL = [NSURL fileURLWithPath:fileTmpPathString];
        fileSuccessViewController *pfVC = [segue destinationViewController];
        pfVC.filePathURL = fileURL;
    }
}

我建议您重写此代码。我不喜欢您处理错误的方式。因为 writefilesuccess 在上传内容时具有未调的值。

我不明白文件是写的。无论如何,请记住,如果您尝试派遣到主线程,则应始终首先检查是否已经在主线程上,因为它可以挂起应用程序。示例:

if([NSThread isMainThread]){
   //perform gui operation
} else {
   dispatch_async(dispatch_get_main_queue(), ^{
      //perfrom gui operation
   });
}

编辑:您应该考虑使用dispatch_sync而不是用于主线程的dispatch_async,因为您在 - (ibaction)filebtnaction中嵌套了调度:( uibutton *)发件人:

    [self doComplexOperateAction];
    dispatch_**sync**(dispatch_get_main_queue(), ^{
        fileHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        fileHud.label.text = @"file operating....";
        fileHud.userInteractionEnabled = NO;
        });
    });

相关内容

最新更新