2015-02-23 34 views
1

我的函数必须创建一个目录,并将整个文件夹层次结构从另一个目录复制到这个新目录中。所有的操作都是异步完成的,但是我希望这个函数返回一个Future,当我调用.then(result)时,它将完成所有的异步工作。我应该在这个Future系列函数中把completer.complete()放在哪里?

但我不知道我应该把我的completer.complete()达到那个位置。

Future<Directory> createCopyDirectory(Directory directoryToCreate){ 
    Completer<Directory> completer = new Completer<Directory>(); 
    completer.complete(
     directoryToCreate.create().then((directory){ 
     Directory contentToCopy = new Directory(globalPathOfDirectoryToCopy); 
     List<Future> creatingContent = new List<Future>(); 
     contentToCopy.list(recursive:true, followLinks:false).forEach((f){ 
      if (f is File){ 
      File fileToCreate = new File(f.path.replaceFirst('pages', userID)); 
      creatingContent.add(fileToCreate.create(recursive:true).then((_){ 
       f.readAsString().then((fileContent){ 
       fileToCreate.writeAsString(fileContent); 
       }); 
      })); 
      } 
     }); 
     return Future.wait(creatingContent).then((_){ return directoryToCreate;}); 
    }) 
); 
    return completer.future; 
} 

我准确的,我的功能工作像预期的,但是,如果我尝试在then()调用直接访问我应该在这个功能所创建的内容,如,飞镖给我的厚望像我有没有创建了内容。所以completer.complete()肯定放置不好,并且在创建内容之前先打电话then()

我试过了的completer.complete()return directoryToCreate替换completer.complete(directoryToCreate)但结果是一样的。

对于在这种情况下构建合适的基于Future的功能,我有点困惑。

回答

1

您不应该在这里需要Completer

Future<Directory> createCopyDirectory(Directory directoryToCreate) { 
    return directoryToCreate.create().then((directory) { 
    String userID = split(userDirectory.path).last; 
    Directory contentToCopy = new Directory(globalPathOfDirectoryToCopy); 
    List<Future> creatingContent = new List<Future>(); 
    return contentToCopy 
     .list(recursive: true, followLinks: false) 
     .forEach((File f) { 
     if (f is File) { 
     File fileToCreate = new File(f.path.replaceFirst('pages', userID)); 
     creatingContent.add(fileToCreate.create(recursive: true).then((_) { 
      return f.readAsString().then((fileContent) { 
      return fileToCreate.writeAsString(fileContent); 
      }); 
     })); 
     } 
    }).then((_) { 
     return Future.wait(creatingContent).then((_) { 
     return directoryToCreate; 
     }); 
    }); 
    }); 
} 

只是为了演示了如何使用的Completer

Future<Directory> createCopyDirectory(Directory directoryToCreate) { 
    Completer<Directory> completer = new Completer<Directory>(); 
    directoryToCreate.create().then((directory) { 
    String userID = split(userDirectory.path).last; 
    Directory contentToCopy = new Directory(globalPathOfDirectoryToCopy); 
    List<Future> creatingContent = new List<Future>(); 
    contentToCopy.list(recursive: true, followLinks: false).forEach((f) { 
     if (f is File) { 
     File fileToCreate = new File(f.path.replaceFirst('pages', userID)); 
     creatingContent.add(fileToCreate.create(recursive: true).then((_) { 
      return f.readAsString().then((fileContent) { 
      return fileToCreate.writeAsString(fileContent); 
      }); 
     })); 
     } 
    }).then((_) => Future 
     .wait(creatingContent) 
     .then((_) => completer.complete(directoryToCreate))); 
    }); 
    return completer.future; 
} 
+0

感谢,顺便说一下! – fandegw 2015-02-23 16:14:55

+1

我更新了我的答案(只有第一部分)。在'contentToCopy'和'.then'之前有一个额外的'return','foreEach'末尾有一个'...' – 2015-02-23 16:28:34

+1

当你评论它时,我发现它是正确的,非常感谢你的大脑工作,它促使我发现问题更具活力 – fandegw 2015-02-23 16:34:26

相关问题