Can CoreStore fetch all unique entity atributes? #274

Closed
opened 2025-12-29 15:28:03 +01:00 by adam · 4 comments
Owner

Originally created by @OlionCompany on GitHub (May 23, 2019).

How can I get all the unique attributes from an Entity?
E.g.:
EntityA = "a: String?, b: Int?, entityB: EntityB?"
EntityB = "a: String?, b: Int?"

My code:

let result = try self.dataStack.fetchAll(From<EntityA>()
    .where(clauses)
    .tweak {
        $0.resultType = .dictionaryResultType
        $0.propertiesToFetch = ["entityB"]
        $0.returnsDistinctResults = true
})

fetchAll waits for [EntityA] and crash to return

Originally created by @OlionCompany on GitHub (May 23, 2019). How can I get all the unique attributes from an Entity? E.g.: EntityA = "a: String?, b: Int?, entityB: EntityB?" EntityB = "a: String?, b: Int?" My code: ``` let result = try self.dataStack.fetchAll(From<EntityA>() .where(clauses) .tweak { $0.resultType = .dictionaryResultType $0.propertiesToFetch = ["entityB"] $0.returnsDistinctResults = true }) ``` fetchAll waits for [EntityA] and crash to return
adam added the question label 2025-12-29 15:28:04 +01:00
adam closed this issue 2025-12-29 15:28:04 +01:00
Author
Owner

@JohnEstropia commented on GitHub (May 24, 2019):

You can use queryAttributes(...) for this. See https://github.com/JohnEstropia/CoreStore#querying (Specifically the entries on queryAttributes(...))

@JohnEstropia commented on GitHub (May 24, 2019): You can use `queryAttributes(...)` for this. See https://github.com/JohnEstropia/CoreStore#querying (Specifically the entries on `queryAttributes(...)`)
Author
Owner

@OlionCompany commented on GitHub (May 24, 2019):

I tried and it doesn't work
СoreStore can't do requests of the attributes with a check for uniqueness.

@OlionCompany commented on GitHub (May 24, 2019): I tried and it doesn't work СoreStore can't do requests of the attributes with a check for uniqueness.
Author
Owner

@JohnEstropia commented on GitHub (May 24, 2019):

Sorry, I'm not sure I understood your question then. Your example says

E.g.:
EntityA = "a: String?, b: Int?, entityB: EntityB?"
EntityB = "a: String?, b: Int?"

so I assumed you were querying properties. Can you give a concrete example of data and what results you are after?

@JohnEstropia commented on GitHub (May 24, 2019): Sorry, I'm not sure I understood your question then. Your example says > E.g.: > EntityA = "a: String?, b: Int?, entityB: EntityB?" > EntityB = "a: String?, b: Int?" so I assumed you were querying properties. Can you give a concrete example of data and what results you are after?
Author
Owner

@OlionCompany commented on GitHub (May 29, 2019):

This question can be closed, I think I figured it out.
If you're trying to extract query attribute from EntityA as other CoreData Entity -> result: Array of [String : NSManagedObjectID].
After conversion we need to fetch entities for this ids. Example:

let values = try self.dataStack.queryAttributes(From<EntityA>(),
                                                Select(#keyPath(EntityA.entityB)),
                                                Where(\.entityB != nil && ...),
                                                GroupBy(\EntityA.entityB),
                                                OrderBy(.ascending(\EntityA.entityB?.name)),
                                                Tweak { $0.returnsDistinctResults = true })
let ids: [NSManagedObjectID] = values.compactMap { $0.first?.value as? NSManagedObjectID }
let result: [EntityB] = self.dataStack.fetchExisting(ids)

In Tweak we can add { $0.resultType = .managedObjectResultType }) for fetch Entities instead Ids, but, app crashes.

@OlionCompany commented on GitHub (May 29, 2019): This question can be closed, I think I figured it out. If you're trying to extract query attribute from EntityA as other CoreData Entity -> result: Array of [String : NSManagedObjectID]. After conversion we need to fetch entities for this ids. Example: ``` let values = try self.dataStack.queryAttributes(From<EntityA>(), Select(#keyPath(EntityA.entityB)), Where(\.entityB != nil && ...), GroupBy(\EntityA.entityB), OrderBy(.ascending(\EntityA.entityB?.name)), Tweak { $0.returnsDistinctResults = true }) let ids: [NSManagedObjectID] = values.compactMap { $0.first?.value as? NSManagedObjectID } let result: [EntityB] = self.dataStack.fetchExisting(ids) ``` In Tweak we can add { $0.resultType = .managedObjectResultType }) for fetch Entities instead Ids, but, app crashes.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#274