2017-04-07 87 views
0

我使用以下代碼從我的稱爲行業的CoreDataModel中取消屬性。不過我能夠使用fetchRequest.propertiesToFetch訪問還沒有要求這些屬性 讓fetchRequest = NSFetchRequest(的entityName: 「工業」) fetchRequest.propertiesToFetch = 「ID」, 「industry_name」]獲取CoreData中的特定屬性或屬性

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 
    } 
+0

請分享您的代碼 –

+0

當您設置的屬性來獲取,但仍取型工業的對象,但會嘗試加快只讓每個請求的prorties抓取對象...這會導致元組= [行業]不是[(id,Industry_name)] – SeanLintern88

+0

您可以通過將獲取請求的'resultType'指定爲'.DictionaryResultType'來修改此行爲,在這種情況下,獲取返回一個數組字典(不是元組),每個字典都有'propertiesToFetch'中指定的鍵和相應的屬性值。 – pbasdf

回答

0

這是如何工作的。當您設置propertiesToFetch CoreData將返回ManagedObject作爲部分故障。這意味着一些屬性被填充了一些不是。當您訪問未填充的屬性時,核心數據將爲您完成,但可能會對性能產生影響,因爲在大多數情況下,需要往返行緩存。

欲瞭解更多信息穀歌爲CoreData Faulting