完成任务之前完成任务的完成处理程序

时间:2017-01-05 02:12:05

标签: swift completionhandler

我真的把自己绑在一个结在这里弄得很困惑,如何按照我希望它们发生的顺序执行任务。

我有从服务器数据构建数组并返回结果的函数。 - 这样可以正常工作并返回结果。

完成此事后(两次)我想运行另一个功能。由于某种原因,我试图将这些函数嵌入到另一个函数中,其中一个完成块一旦这两个函数完成并返回数据就会返回true,但我正在尝试这个函数,它在函数完成之前返回结果

@Autowired
private Session session;

...

// Option 1: write some cypher
Map<String, Object> params = new HashMap<>():
params.put("ids", ids) // use a stream like above to collect the ids.
Iterable<SomeObject> someObjects = session.query(SomeObject.class, "MATCH (n:SomeObject) ...", params);

//Option 2: use the load by instances method which also allows pagination:
Collection<SomeObject> someObjects = session.loadAll(categories, new Pagination(0, 25));

1 个答案:

答案 0 :(得分:2)

将下一步移动到前面的闭包中。继续筑巢。像这样:

func getCollectionArrays(_ block: ((Bool) -> Void)? = nil) {
    var resultPack: Bool!
    var resultPart: Bool!
    BuildArray.buildArrayFromQuery(queryForCollection: "Pack", sender: self, completeBlock: { (result) in
        if result != nil {
            resultPack = true
        }
        // next step, nested in this closure
        BuildArray.buildArrayFromQuery(queryForCollection: "Part", sender: self, completeBlock: { (result) in
            if result != nil {
                resultPart = true
            }
            // next step, nested in _this_ closure
            if resultPack == true && resultPart == true {
                block!(true)
            }
        })
    })
}
相关问题