CoreStore query multiple entities #138

Closed
opened 2025-12-29 15:25:19 +01:00 by adam · 3 comments
Owner

Originally created by @martheli on GitHub (May 24, 2017).

I have read over the readme but I have not seen any examples of how to pull data from two entities if there is a relationship. For example if I had one entity that was Manager and one that was Employee. I wanted the result output to show the employee and the employee's manager. Is this type of query supported in CoreStore?

Originally created by @martheli on GitHub (May 24, 2017). I have read over the readme but I have not seen any examples of how to pull data from two entities if there is a relationship. For example if I had one entity that was Manager and one that was Employee. I wanted the result output to show the employee and the employee's manager. Is this type of query supported in CoreStore?
adam added the question label 2025-12-29 15:25:19 +01:00
adam closed this issue 2025-12-29 15:25:19 +01:00
Author
Owner

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

I'm not sure I understood your question. You can fetch the employee and then access the manager for that employee. So if you have these entities:

class Employee: NSManagedObject {
    // ...
    @NSManaged var manager: Manager?
}
class Manager: NSManagedObject {
    // ...
    @NSManaged var employee: Employee?
}

You can fetch the employee

let employee = CoreStore.fetchOne(
    From<Employee>(),
    Where( ... ) // some condition
)

and then access the manager

let manager = employee.manager

Does this answer your question?

@JohnEstropia commented on GitHub (May 24, 2017): I'm not sure I understood your question. You can fetch the employee and then access the manager for that employee. So if you have these entities: ```swift class Employee: NSManagedObject { // ... @NSManaged var manager: Manager? } class Manager: NSManagedObject { // ... @NSManaged var employee: Employee? } ``` You can fetch the employee ``` let employee = CoreStore.fetchOne( From<Employee>(), Where( ... ) // some condition ) ``` and then access the manager ```swift let manager = employee.manager ``` Does this answer your question?
Author
Owner

@martheli commented on GitHub (May 24, 2017):

Ok yes that does answer it. Do you need to set up the entities as classes like you show above for it to work or can I use the xcmappingmodel file to set up the entities only?

@martheli commented on GitHub (May 24, 2017): Ok yes that does answer it. Do you need to set up the entities as classes like you show above for it to work or can I use the xcmappingmodel file to set up the entities only?
Author
Owner

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

You can create the xcdatamodeld file and let Xcode automatically generate the class code for you.

@JohnEstropia commented on GitHub (May 24, 2017): You can create the xcdatamodeld file and let Xcode automatically generate the class code for you.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#138