使用Swift使用CoreData检索已保存的图像数据时出错

时间:2016-12-16 10:50:46

标签: ios swift xcode core-data firebase-storage

我已经存储了一个名为" Cards"的类的类数据。进入核心数据。 "卡" class包括存储图像数据的图像的字段。我似乎能够成功地将数据保存到核心数据(至少没有错误),但是当试图访问该数据时,我收到一个未知错误。

这是我将每张卡保存到核心数据的保存方法。它似乎工作正常。

func save(toBeSaved: Cards) {
    //1
    let appDelegate =
        UIApplication.shared.delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    //2
    let entity =  NSEntityDescription.entity(forEntityName: "CardEntity",
                                             in:managedContext)

    let card = NSManagedObject(entity: entity!,
                                 insertInto: managedContext)

    //3
    card.setValue(toBeSaved.name, forKey: "name")
    card.setValue(toBeSaved.imageName, forKey: "imageName")
    card.setValue(toBeSaved.manaCost, forKey: "manaCost")
    card.setValue(toBeSaved.rarity, forKey: "rarity")
    card.setValue(toBeSaved.whichClass, forKey: "whichClass")
    card.setValue(toBeSaved.cardType, forKey: "cardType")
    card.setValue(toBeSaved.tribe, forKey: "tribe")
    card.setValue(toBeSaved.cardText, forKey: "cardText")
    card.setValue(toBeSaved.set, forKey: "set")

    //4
    do {
        try managedContext.save()
        //5
        print("Feed: successfully saved")
        nsAllCards.append(card)
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    }
}

以下是尝试从核心数据中获取已保存信息的代码。

// fetch cards and populate
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    //2
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "CardEntity")

    //3
    do {
        let results =
            try managedContext.fetch(fetchRequest)
        nsAllCards = results as! [NSManagedObject]
    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }
    print("Feed: core data contains \(nsAllCards.count) elements")

    if nsAllCards.count > 0 {
        for item in nsAllCards {
            print("Feed: \(item)")
            print("Feed: \(item.value(forKey: "name") as! String)")
            print("Feed: \(item.value(forKey: "imageName") as! Data)")
            print("Feed: \(item.value(forKey: "manaCost") as! Int)")
            print("Feed: \(item.value(forKey: "rarity") as! String)")
            print("Feed: \(item.value(forKey: "whichClass") as! String)")
            print("Feed: \(item.value(forKey: "cardType") as! String)")
            print("Feed: \(item.value(forKey: "tribe") as! String)")
            print("Feed: \(item.value(forKey: "cardText") as! String)")
            print("Feed: \(item.value(forKey: "set") as! String)")
            let tempCard = Cards(name: item.value(forKey: "name") as! String, imageName: item.value(forKey: "imageName") as! Data, manaCost: item.value(forKey: "manaCost") as! Int, rarity: item.value(forKey: "rarity") as! String, whichClass: item.value(forKey: "whichClass") as! String, cardType: item.value(forKey: "cardType") as! String, tribe: item.value(forKey: "tribe") as! String, cardText: item.value(forKey: "cardText") as! String, set: item.value(forKey: "set") as! String)
            allCards.append(tempCard)
        }
    }

我添加了所有这些打印行,以便试图弄清楚错误的位置,并且它总是在print imageName行(它保存从firebase存储下载的图像的数据)处停止。 正如您可以从代码中看出来的那样,在打印行之后,我正在尝试使用从核心数据中获取的数据构造一个对象,这样我就可以使用NSManagedObject而不是一个普通的&#34;卡片&#34;要使用的对象。

我是核心数据的新手,我不确定这是否是保存从firebase存储下载的图像的正确方法。请以任何方式帮助您。

我的实体代码:

extension CardEntity {

@nonobjc public class func fetchRequest() -> NSFetchRequest<CardEntity> {
    return NSFetchRequest<CardEntity>(entityName: "CardEntity");
}

@NSManaged public var name: String?
@NSManaged public var imageName: NSData?
@NSManaged public var manaCost: Int16
@NSManaged public var rarity: String?
@NSManaged public var whichClass: String?
@NSManaged public var cardText: String?
@NSManaged public var tribe: String?
@NSManaged public var cardType: String?
@NSManaged public var set: String?

}

0 个答案:

没有答案