【问题标题】:Fetching particular attributes or properties in CoreData在 CoreData 中获取特定的属性或属性
【发布时间】:2017-09-02 10:18:22
【问题描述】:

我正在使用以下代码来消除属性,同时从名为行业的 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
    }

【问题讨论】:

  • 请分享您的代码
  • 当您将属性设置为 fetch 时,它仍然会获取 Industry 类型的对象,但会尝试通过仅获取每个对象上请求的属性来加快获取速度...这会导致 tuples = [行业] 不是 [(id, Industry_name)]
  • 您可以通过将获取请求的resultType 指定为.DictionaryResultType 来修改此行为,在这种情况下,获取返回一个字典数组(不是元组),每个字典都有指定的键在propertiesToFetch 和相应的属性值中。

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


【解决方案1】:

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

更多信息谷歌CoreData Faulting

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多