簡體   English   中英

CoreData 獲取 Attribute 的不同值

[英]CoreData get distinct values of Attribute

我正在嘗試將我的NSFetchRequest設置為核心數據以檢索實體中特定屬性的唯一值。 IE

具有以下信息的實體:

  name | rate | factor |
_______|______|________|
John   |  3.2 |    4   |
Betty  |  5.5 |    7   |
Betty  |  2.1 |    2   |
Betty  |  3.1 |    2   |
Edward |  4.5 |    5   |
John   |  2.3 |    4   |

我將如何設置請求以僅返回一個數組:John、Betty、Edward?

您應該使用后備存儲來幫助您獲得不同的記錄。

如果你想得到一個只有 John、Betty、Edward 的數組,你可以這樣做:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.managedObjectContext];

// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work. 
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct names, only ask for the 'name' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]];
fetchRequest.returnsDistinctResults = YES;

// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);

這是 Swift 5.x 中的一種簡單方法:

// 1. Set the column name you want distinct values for
let column = "name"

// 2. Get the NSManagedObjectContext, for example:
let moc = persistentContainer.viewContext

// 3. Create the request for your entity
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "User")

// 4. Use the only result type allowed for getting distinct values
request.resultType = .dictionaryResultType

// 5. Set that you want distinct results
request.returnsDistinctResults = true

// 6. Set the column you want to fetch
request.propertiesToFetch = [column]

// 7. Execute the request. You'll get an array of dictionaries with the column
// as the name and the distinct value as the value
if let res = try? moc.fetch(request) as? [[String: String]] {
    print("res: \(res)")

    // 8. Extract the distinct values
    let distinctValues = res.compactMap { $0[column] }
}

您正在嘗試將 Core Data 用作過程數據庫,而不是 API 預期的 object 圖形管理器,因此您找不到一種簡單的方法來執行此操作。

在 Core Data 中沒有直接的方法可以做到這一點,因為 Core Data 關注的是對象而不是值。 由於托管對象保證是唯一的,Core Data 不太關心每個對象的值或者它們是否重復或其他對象的值。

要查找唯一值:

  1. 按特定值執行提取 這將為您提供一個字典數組,其中包含鍵name和名稱字符串本身的值。
  2. 在 (1) 中返回的數組上使用集合集合運算符返回一組唯一值。

所以,像這樣:

NSSet *uniqueNames=[fetchedNameDicts valueForKeyPath:@"@distinctUnionOfSets.name"];

... 這將返回一組 NSString 對象,所有對象都具有唯一值。

我正在嘗試將我的NSFetchRequest設置為核心數據,以檢索實體中特定屬性的唯一值。 IE

具有以下信息的實體:

  name | rate | factor |
_______|______|________|
John   |  3.2 |    4   |
Betty  |  5.5 |    7   |
Betty  |  2.1 |    2   |
Betty  |  3.1 |    2   |
Edward |  4.5 |    5   |
John   |  2.3 |    4   |

我將如何設置請求以僅返回數組:John、Betty、Edward?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM