我想獲得核心數據管理對象的屬性的總和,但似乎無論什麼我做我得到0斯威夫特3核心數據NSExpression總是返回0
我的目標試圖獲得我所有商機對象上的估計值屬性的總和。 estimatedValue屬性是Core Data中的一個小數。
這裏是我當前的代碼版本:
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Opportunity")
fetchRequest.resultType = .dictionaryResultType
fetchRequest.returnsObjectsAsFaults = false
let expression = NSExpressionDescription()
expression.expression = NSExpression(forFunction: "sum:", arguments:[NSExpression(forKeyPath: "estimatedValue")])
expression.name = "sumOfEstimatedValue";
expression.expressionResultType = .decimalAttributeType
fetchRequest.propertiesToFetch = [expression];
do {
let opportunityAggregates = try stack.context.fetch(fetchRequest)
print(opportunityAggregates)
} catch {
print("Failed to fetch aggregates")
}
上面的代碼的結果是:
[{
sumOfEstimatedValue = 0;
}]
這裏是一個打印出來保存在應用程序中的機會:
[<Opportunity: 0x60800009dd80> (entity: Opportunity; id: 0x60800002d8c0 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C17> ; data: {
company = nil;
contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
createdDate = "2017-09-03 22:40:00 +0000";
estimatedValue = "42.41";
opportunityDescription = nil;
projectedCloseDate = nil;
stage = "0x60800002d860 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C16>";
}), <Opportunity: 0x60800009dc90> (entity: Opportunity; id: 0x60800002d380 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C15> ; data: {
company = nil;
contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
createdDate = "2017-09-03 22:40:00 +0000";
estimatedValue = "42.41";
opportunityDescription = nil;
projectedCloseDate = nil;
stage = "0x60800002d440 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C14>";
}), <Opportunity: 0x60800009d9c0> (entity: Opportunity; id: 0x60800002b8e0 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C9> ; data: {
company = nil;
contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
createdDate = "2017-09-03 22:40:00 +0000";
estimatedValue = "42.41";
opportunityDescription = nil;
projectedCloseDate = nil;
stage = "0x60800002aaa0 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C8>";
}), <Opportunity: 0x60800009dab0> (entity: Opportunity; id: 0x60800002c580 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C11> ; data: {
company = nil;
contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
createdDate = "2017-09-03 22:40:00 +0000";
estimatedValue = "42.41";
opportunityDescription = nil;
projectedCloseDate = nil;
stage = "0x60800002d3e0 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C10>";
}), <Opportunity: 0x60800009dba0> (entity: Opportunity; id: 0x60800002d140 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C13> ; data: {
company = nil;
contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
createdDate = "2017-09-03 22:40:00 +0000";
estimatedValue = "42.41";
opportunityDescription = nil;
projectedCloseDate = nil;
stage = "0x60800002c140 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C12>";
}), <Opportunity: 0x60800009d3d0> (entity: Opportunity; id: 0x60800002b3a0 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C7> ; data: {
company = nil;
contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
createdDate = "2017-09-03 22:40:00 +0000";
estimatedValue = "42.41";
opportunityDescription = nil;
projectedCloseDate = nil;
stage = "0x60800002abe0 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C6>";
})]
非常感謝您提供任何幫助。
你可以嘗試評論這行'fetchRequest.propertiesToFetch = [表達式];'然後請發佈你的輸出。 –
@ViniApp包含打印出應用程序中商機的第三個代碼塊是我在執行此操作時得到的結果。我通過註釋掉那行和'fetchRequest.resultType = .dictionaryResultType'得到了這些結果。我不知道爲什麼相同的fetchRequest返回所有內容,但在與表達式一起使用時返回不正確的結果。我對iOS開發很陌生,所以可能會缺少某些東西,但我認爲這看起來像我所看到的所有教程和示例。 –
是由SQL存儲還是內存存儲支持的核心數據設置? –