如何获取与刚刚保存到Core Data完全相同的NSManagedObject?

时间:2017-02-15 18:44:21

标签: ios swift core-data magicalrecord

我只是这样做:

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))
    }
}

0 个答案:

没有答案