2016-11-21 108 views
0

我嘗試使用下面的代碼來從核心數據特定屬性:的Xcode 8.1的核心數據獲取特定屬性

NSFetchRequest *test = [[NSFetchRequest alloc] init]; 
test.entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.currentMainContext]; 
test.predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"]; 
test.resultType = NSDictionaryResultType; 
test.returnsDistinctResults = YES; 
test.propertiesToFetch = @[@"property"]; 

NSArray *results = [self.currentMainContext executeFetchRequest:test error:error]; 

,但我得到一個空數組回來。

如果我註釋掉test.resultType = NSDictionaryResultType;然後我回來的所有實體的數組在我的數據庫如預期。關於NSDictionaryResultType這是什麼錯誤?

回答

1

獲取與的NSDictionaryResultTyperesultType請求具有一個特點。
DocumentationincludesPendingChanges指出:

如果該值爲NO,獲取請求不檢查未保存的更改,並只返回持久存儲相匹配的謂詞對象。

YES的值被不與結果類型NSDictionaryResultType,包括的彙總結果(如最大和最小)計算結合的支持。對於字典,從提取返回的數組反映持久存儲中的當前狀態,並且不考慮上下文中的任何掛起的更改,插入或刪除。

因此,請確保您已保存更改或使用NSManagedObjectResultType