|
Here is another CoreData question (sorry), after @Obelix has kindly helped me with the last one. My model looks like this: BrandsEntity (One-to-many relationship to DealersEntity) DealersEntity (One-to-many relationship to: CarsEntitiy / Many-to-One relationship to: BrandEntity) CarsEntity (Many-to-One relationship to DealersEntity) Now, cars can either be reserved or not. I can calculate the number of available cars and show the result per Dealer. When I try to show the number of available cars per Brand, though, the app crashes on launch: Thread 1: "-[DealersEntity cars]: unrecognized selector sent to instance 0x600002a1a440" During my journey with CoreData I've come along a few of those instances where I'd get a crash stating: unrecognized selector. Most times, if not all, it was down to me doing silly things. Here, though, I can't find the missing link. Here is how I calculate the number of available cars per Dealer
and here is how I try the same thing per Brand. The calculation is exactly the same, the issue seems to be in the relationship in that BrandsEntity doesn't have a direct relationship to Cars (only the DealersEntity in the middle has). Either that or this line is total nonsense of mine:
Here are my three views in hierarchical order:
Can anyone spot the issue? Or if that's the case, send me a hint as to how I can access the "grandchild" CarsEntity from the Parent "BrandsEntity"? This would greatly help me with my journey. I've tried to find a solution online, but it looks like I have to craft a whole essay in order to express what I'm looking for as I have to work on my coding terminology skills ... Many thanks in advance! |
|
|
|
What is the name of the relationship between Looking at your data model I recognized that how the relationship is set up every dealer would use the exactly same identical car and not a own instance of it. I'm not sure if this is what you want. |
|
hi, this expression is suspect:
when computed for a consider doing something more direct, and leverage what you already have. for a given object
example usage:
hope that helps, DMG |
|
@Hatsushira
The name of the relationship is "cars" so I think that this should be correct, no?
I'm not exactly sure how I would do that? Could you give me a nudge in the right direction here? So far I got the issue solved with a slight variation of @delawaremathguy 's solution, which I'll post below. |
|
@delawaremathguy Your solution didn't work in the first place, but it greatly helped me getting there! I think when I tried to call it in BrandsView, I got an error like "Instance method 'appendInterpolation(_:formatter:)' requires that '[CarsEntity]' inherit from 'NSObject'. Searching for this issue told me to wrap the call as a String like this:
With that in place, my List showed both Brands I'd saved together with all cars per brand as well as core data addresses (i guess?) like <x-coredata: //2E58640.....> all in one huge cell per brand. Anyway, I didn't want to give up so I finally worked my way to the following solution:
which I could call without any issues:
As you can see I had to change the type of carsArray to Int in order to be able to calculate the difference between allCars and reservedCars. This shouldn't be an issue, right? It's working just fine but I'm not sure if I may be missing something that could hit me later on. Last but not least: Many, many thanks! This has helped me tremendously both with this very issue and also getting a better understanding of how to work with core data relationships! |
|
@BenjiTerv Sorry, I was away over the weekend.
Basically, you would need an entity between There are other possible solutions but this depends on what use case you want to model and which problem your app should solve. |
|
|
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.