2015-10-28 29 views
0

我需要獲取具有所有屬性的實體期望一個屬性。我知道有辦法,包括所有屬性的名稱,做這樣的事情:排除NSFetchRequest中的一個屬性

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

[request setResultType:NSDictionaryResultType]; 
[request setPropertiesToFetch: 
[NSArray arrayWithObjects:@"property1", @"property2", /* etc. */ nil]]; 

NSEntityDescription *e = [NSEntityDescription entityForName:entityName 
            inManagedObjectContext:self.context]; 

但我並不想提一下所有,因爲一個屬性的屬性! 你知道這個有什麼好的性能解決方案嗎?

+1

爲什麼要排除一個屬性?如果是內存優化,爲了避免加載像圖像這樣的大型屬性,您可以爲該屬性創建一個單獨的實體,並與您的現有實體建立1:1關係。然後,您可以在不擔心內存的情況下獲取您的現有實體,並且只在需要時才抓取相關實體。 – pbasdf

+0

pbasfd!我有blob圖像,它會導致高內存使用量。另外,看起來setPropertiesToFetch不起作用,並且在滾動所有uitableview時內存使用情況與之前的相同。 –

+0

@pbasdf - 你有正確的答案。不幸的是,OP提出了錯誤的問題;-) –

回答

1

不幸的是,你將不得不命名除一個之外的所有屬性。沒有其他辦法。這是一種自動執行並不擔心性能的方法。

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.context]; 
NSArray *allProperties = entityDescription.properties; 
NSMutableArray *propertiesToFetch = [NSMutableArray arrayWithCapacity:allProperties.count]; 
for (NSPropertyDescription *property in allProperties) { 
    if (![property.name isEqualToString:@"xxx"]) { 
     [propertiesToFetch addObject:property]; 
    } 
} 

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName]; 
request.resultType = NSDictionaryResultType; 
request.propertiesToFetch = propertiesToFetch; 
-1

您可以使用此方法來從所有屬性的數組中刪除特定的屬性。假設您的實體名爲PatientRecord

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

[request setResultType:NSDictionaryResultType]; 

PatientRecord *patient; 
NSMutableArray *allProperties = [[NSMutableArray alloc] initWithArray:patient.entity.properties]; 
[allProperties removeObject:@"propertyToRemove"]; 

[request setPropertiesToFetch:allProperties]; 

NSEntityDescription *e = [NSEntityDescription entityForName:entityName 
           inManagedObjectContext:self.context]; 
+0

不工作,請檢查關於'NSEntityDescription'的'屬性'屬性的文檔。 – deadbeef

+0

請解釋爲什麼它不起作用的原因?我已經使用過這個(以更多的方式)。我應該在文檔中尋找什麼? – Nishant

+0

'properties'數組不包含字符串,而是'NSPropertyDescription'子類。看到我的答案。 – deadbeef

相關問題