【问题标题】:MagicalRecord completion block is not called under test target在测试目标下未调用 MagicalRecord 完成块
【发布时间】:2015-08-07 07:37:10
【问题描述】:
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
})

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

MagicalRecord.setupCoreDataStackWithInMemoryStore()

【问题讨论】:

    标签: ios swift unit-testing magicalrecord


    【解决方案1】:

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

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

    您能否尝试将saveWithBlock: 方法更改为saveWithBlockAndWait:?它是同步执行的。还是等着用XCTestExpectation执行异步调用?

    【讨论】:

      猜你喜欢
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 2018-05-10
      • 2015-09-08
      • 1970-01-01
      相关资源
      最近更新 更多