0

我正在使用以下代码来消除属性,同时从名为行业的 CoreDataModel 中获取。我仍然可以使用fetchRequest.propertiesToFetch访问那些没有请求的属性 let fetchRequest = NSFetchRequest(entityName: "Industry") fetchRequest.propertiesToFetch = ["id","industry_name"]

    After this i am using following code:
    industryObject.industry_name=tuple.value(forKey: ""ind_subtype"") as? String

    "ind_subtype" i have not specified in *.propertiesToFetch* and i am still able to access it


func fetchIndustryNamesWithId()->[IndustryData]{

        var industryData=[IndustryData]()
        //fetchRequest.predicate = NSPredicate(format: "firstName == %@", firstName)
        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Industry")
        fetchRequest.propertiesToFetch = ["id","industry_name"]
        do {
            let tuples = try managedObjectContext.fetch(fetchRequest)
            for tuple in tuples{
                let industryObject=IndustryData()
                industryObject.id=tuple.value(forKey: "id") as? Int
                industryObject.industry_name=tuple.value(forKey: "industry_name") as? String
                industryObject.ind_subtype=tuple.value(forKey: "ind_subtype") as? String
                industryData.append(industryObject)
            }
            return industryData
        } catch {
            let fetchError = error as NSError
            print(fetchError)
        }
        return industryData
    }
4

1 回答 1

0

这就是它的工作原理。当您设置CoreData 时,propertiesToFetch将返回 ManagedObject 作为部分故障。这意味着某些属性已填充,有些则未填充。当您访问未填充的属性时,核心数据将为您完成它,但它可能会对性能产生影响,因为在大多数情况下需要往返行缓存。

欲了解更多信息谷歌CoreData Faulting

于 2017-04-07T08:53:15.447 回答