问题
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()
回答1:
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
Could you try to change the saveWithBlock:
method to saveWithBlockAndWait:
? It is executed syncronously. Or wait to execute asynchronous call with XCTestExpectation
?
来源:https://stackoverflow.com/questions/31872105/magicalrecord-completion-block-is-not-called-under-test-target