Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

全选删除文件数组越界,单个删除没有问题 #58

Open
anotherL opened this issue Aug 6, 2019 · 0 comments
Open

全选删除文件数组越界,单个删除没有问题 #58

anotherL opened this issue Aug 6, 2019 · 0 comments
Labels
bug Something isn't working

Comments

@anotherL
Copy link

anotherL commented Aug 6, 2019

你好作者,我发现在文件管理类名:LLSandboxVC.m 那里有个问题 ,同时自己重新写UI做了一些修改,写得有点囊肿,望采纳。

测试结果:全选项目的时候,执行操作删除的话,会出现数组越界的情况,单个选择的删除的话没有问题。

方法出错在:

  • (BOOL)deleteFile:(NSIndexPath *)indexPath 这里方法里的
    LLSandboxModel *model = self.sandboxModel.subModels[indexPath.row];
    这里取值的时候。

个人解决方案=======》》》》

设想,在尽量不改原来代码的情况下做另外操作:
情况1:当多个文件删除时,当数组对象执行removeObject删除后,数组下标会发生变化。我们不应再以下标取值,而是先把要删除的对象取出来,再执行removeObject删除对应的选项,再刷新UI。
情况2:当删除多个文件的话是否因文件过大、较多而卡UI呢?考虑异步删除。

`//开始删除部分

  • (void)deleteFilesWithIndexPaths:(NSArray *)indexPaths {
    if (indexPaths.count == 1) {
    for (NSIndexPath *indexPath in indexPaths) {
    [self deleteFile:indexPath];
    }
    }else if (indexPaths.count > 1){

    [self deleteAllItemFileWithIndexPaths:indexPaths Block:^(BOOL returnCode, NSString *msg) {
    [self.tableView reloadData];
    NSLog(@"%@",msg);
    if (returnCode == NO) {
    [self showAlertControllerWithMessage:msg handler:nil];
    }else{
    [self toastMessage:NSLocalizedString(@"删除成功", nil)];
    }
    }];
    }else{
    NSLog(@"空数组");
    }
    }

///原来的方法

  • (BOOL)deleteFile:(NSIndexPath *)indexPath {
    NSInteger index = indexPath.row;
    NSLog(@"%ld",(long)index);
    BOOL ret = NO;
    if (index < self.sandboxModel.subModels.count && self.sandboxModel.subModels.count != 0) {
    LLSandboxModel *model = self.sandboxModel.subModels[index];
    NSError *error;
    ret = [[NSFileManager defaultManager] removeItemAtPath:model.filePath error:&error];
    if (ret) {
    [self.sandboxModel.subModels removeObject:model];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else {
    [self showAlertControllerWithMessage:[NSString stringWithFormat:@"%@\nFilePath:%@\nError:%@",NSLocalizedString(@"删除文件失败", nil),model.filePath,error.localizedDescription] handler:nil];
    }
    }
    return ret;
    }

//批量删除部分(自己添加的部分)

  • (void)deleteAllItemFileWithIndexPaths:(NSArray *)indexPaths Block:(void (^ __nullable)(BOOL returnCode ,NSString *msg))block{
    __block BOOL ret = NO;
    __block NSString msg = @"";
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    @synchronized (self) {
    NSMutableArray <LLSandboxModel
    > *deleteArray = [NSMutableArray array];
    NSMutableArray *subModels = self.sandboxModel.subModels.mutableCopy;
    for (NSIndexPath *indexPath in indexPaths) {
    NSInteger index = indexPath.row;
    if (index < subModels.count && subModels.count != 0) {
    LLSandboxModel model = subModels[index];
    [deleteArray addObject:model];
    }
    }
    [deleteArray enumerateObjectsUsingBlock:^(LLSandboxModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSLog(@"delete : %@",obj.filePath);
    NSString
    filePath = obj.filePath;
    NSError *error;
    BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
    if (flag) {
    [self.sandboxModel.subModels removeObject:obj];
    ret = YES;
    }else{
    ret = NO;
    msg = [NSString stringWithFormat:@"%@%@",msg,[NSString stringWithFormat:@"%@\nFilePath:%@\nError:%@",NSLocalizedString(@"删除文件失败", nil),filePath,error.localizedDescription]];
    }
    }];
    NSLog(@"%@",subModels);
    dispatch_async(dispatch_get_main_queue(), ^{
    if (block) {
    block(ret,msg);
    }
    });
    }
    });
    }
    `

个人解决方案=======《《《《

@HDB-Li HDB-Li added the bug Something isn't working label Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants