2016-11-06 27 views
0

我已經coreData實體定義爲獲得在CoreData的ios表的只有少數性能swift3

enter image description here

enter image description here

在TTFinds

@nonobjc public class func fetchRequest() -> NSFetchRequest<TTFinds> { 
    return NSFetchRequest<TTFinds>(entityName: "TTFinds"); 
} 

我試圖讓TTFinds列表as

let request: NSFetchRequest<TTFinds> = TTFinds.fetchRequest()   
    do { 
     let result = try appDel.persistentContainer.viewContext.fetch(request) 

可以看出TTFinds獲取其父TTArea對象。不過,我只想得到幾列。假設只有namecreationDate。如何完成?

我已經試過

let request: NSFetchRequest<TTFinds> = TTFinds.fetchRequest() 
     request.propertiesToFetch = NSArray.init(objects: "name", "creationDate", "image", "latitude", "longitude", 
      "area.name") as? [Any];     

然而area.name給出錯誤。

+0

你會得到什麼錯誤? –

+0

@MartinR錯誤提取請求,sigabrt –

回答

0

忘記propertiesToFetch並只是獲取整個對象。

首先,如果您使用結果類型的字典,則只能檢索選擇性屬性。這會使你的代碼更加繁瑣冗長,而且效率也不會提高。其次,如果你只是獲取對象,即使有很多對象,Core Data也會在「底層」下進行各種優化,只是獲取它需要的東西。這是通過訪問您需要的屬性觸發的。因此,假設你只提取一些屬性的目的是爲了節省內存或者減少性能瓶頸,在你實際遇到瓶頸之前不要擔心它。

注意:你的關係應該有反向關係!

相關問題