本文介绍了在测试目标下未调用 MagicalRecord 完成块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MagicalRecord.saveWithBlock({ context in

        if let items = dictionary["items"] as? Array<NSDictionary> {
            for itemInfo in items {
                DBItem.findOrUpdateItemWithDictionary(itemInfo, inContext: context)
            }
        }
        //is called
        if let sets = dictionary["item_sets"] as? Array<NSDictionary> {
            for setInfo in sets {
                DBSet.findOrUpdateSetWithDictionary(setInfo, inContext: context)
            }
        }

        }, completion: { finished, error in

        completionBlock(error) //is not called
})

这是我设置核心数据堆栈的方式:

This is how I setup my core data stack:

MagicalRecord.setupCoreDataStackWithInMemoryStore()

推荐答案

saveWithBlock: 方法是异步执行的.尽管我不知道您的测试代码如何,但测试可能会在调用完成块之前完成.

The saveWithBlock: method is executed asynchronously. The test might be finished before calling completion block though I do not know how is your test code.

https://github.com/magicalpanda/MagicalRecord/blob/c7b14f658b4fca32f8d33f8f76160203053ce4b9/MagicalRecord/Core/MagicalRecord%2BActions.m#L14-L35

您可以尝试将 saveWithBlock: 方法更改为 saveWithBlockAndWait: 吗?它是同步执行的.或者等待使用 XCTestExpectation 执行异步调用?

Could you try to change the saveWithBlock: method to saveWithBlockAndWait:? It is executed syncronously. Or wait to execute asynchronous call with XCTestExpectation?

这篇关于在测试目标下未调用 MagicalRecord 完成块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:14