我只是这样做:
var objectID: NSManagedObjectID!
MagicalRecord.save(block: { context in
if let dictionary = (response as? [AnyHashable: Any])?["data"] as? [AnyHashable: Any] {
if let itemInfo = dictionary["item"] as? [AnyHashable: Any] {
objectID = Item.create(with: itemInfo, in: context).objectID //here objectID is NOT nil
}
}
}, completion: { error in
let item = Item.find(with: objectID) //here item is nil
completion(item, Error(error: error))
})
我还创建了扩展名:
extension NSManagedObject {
class func find(with objectID: NSManagedObjectID) -> Self? {
return mr_findFirst(with: NSPredicate(format: "SELF = %@", objectID))
}
}