获取CoreData中的特定属性或属性

时间:2017-04-07 06:34:02

标签: ios core-data ui-automation swift3.1

我正在使用以下代码来从我称为行业的CoreDataModel中取消属性。我仍然可以使用 fetchRequest.propertiesToFetch 访问那些未请求的属性         让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
    }

1 个答案:

答案 0 :(得分:0)

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

了解更多信息google for CoreData Faulting