使用方法预填充核心数据

时间:2016-12-07 14:37:35

标签: swift core-data

我为我的Core Data子类创建了一个扩展,在这里我实现了一个预先填充表的方法,首先我检查表中是否有任何内容,然后插入一些值,但是当我执行一个时出现错误取:

由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'executeFetchRequest:error: <null> is not a valid NSFetchRequest.'

import Foundation
import CoreData

extension SimulationParams {

    public static func feedSimulationParams(context: NSManagedObjectContext) {


        let fetchRequest : NSFetchRequest<NSFetchRequestResult>

        if #available(iOS 10.0, *) {
            fetchRequest = SimulationParams.fetchRequest()
        } else {
            fetchRequest = NSFetchRequest(entityName: "SimulationParams")
        }

        let idDescriptor: NSSortDescriptor = NSSortDescriptor(key: "id", ascending: false)
        fetchRequest.sortDescriptors = [idDescriptor]

        fetchRequest.fetchLimit = 1


        var newId = 0;

        do {
            let results = try context.fetch(fetchRequest)

            if(results.count == 0){

                self.setValue(getId(fetchRequest: fetchRequest, context: context), forKeyPath: "id")
                self.setValue(1.5, forKeyPath: "bankFeePercentage")
                self.setValue(1, forKeyPath: "consoMaxMonthly")
                self.setValue(12, forKeyPath: "consoMinMonthly")
                self.setValue(3.5, forKeyPath: "consoRatePercentage")
                self.setValue(4, forKeyPath: "fileFeePercentage")
                self.setValue(1, forKeyPath: "immoMaxMonthly")
                self.setValue(25, forKeyPath: "immoMinMonthly")
                self.setValue(2.05, forKeyPath: "immoRatePercentage")
                self.setValue(3, forKeyPath: "prepaymentPenaltyPercentage")
                self.setValue(1, forKeyPath: "notaryGridId")

                do {
                    try context.save()
                } catch let error as NSError {
                    print ("Error first demande insertion \(error)")
                }
            }


        } catch {
            let fetchError = error as NSError
            print(fetchError)
        }



    }
}

1 个答案:

答案 0 :(得分:0)

解决方案:

警告,您必须删除数据模型检查器中的模块并将其设置为全局命名空间。

enter image description here

您必须在代码上执行此操作:

    let fetchRequest : NSFetchRequest<NSFetchRequestResult>

    if #available(iOS 10.0, *) {
        fetchRequest = Params.fetchRequest()
    } else {
        fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Params")
    }

    //let predicate = NSPredicate(format: "key=%@", key)
    //fetchRequest.predicate = predicate

    do {
        let results = try context.fetch(fetchRequest)

        if(results.count != 0){

            let params = NSEntityDescription.insertNewObject(forEntityName: "Params", into: context) as! Params

            params.setValue(value, forKey: "value")

            do {
                try context.save()
            } catch let error as NSError {
                print ("Error first demande insertion \(error)")
            }

        } else if (results.count == 0) {

            let params = NSEntityDescription.insertNewObject(forEntityName: "Params", into: context) as! Params

            params.key = key
            params.value = value

            context.insert(params)
        }


    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }