|
Hello All, I hope someone can help lead me in the right direction. Let me preface this by saying that i am relatively new to Swift, but have a strong programming background. I am looking for way to display entries from a coredata entity in a list view, with like items combined into a sinlge row displaying the combined count as a Quantity. Example: In core data, i may have the following 5 entries (first line contains the attributes) Name / Serial Number / Aquired Date . sprocket A / 001 / 01/01/2020 . sprocket A / 003 / 01/02/2020 . sprocket A / 005 / 01/02/2020 . sprocket B / 0089 / 01/01/2020 . sprocket B / 0142 / 01/02/2020 I would like my list view to display two row as: sprocket A -- Qty: 3 sprocket B -- Qty: 2 Can i structure a FetchRequest to combine the entries is such a manner? Or do i need to fetch all records as normal and build a new array with the combined elements to bind my list view to? Thanks for in advance for the help! I hope i explained it clearly enough. |
|
hi, i think when you ask CoreData to return entities, they pretty much come back as an array of entities. i don't think you can structure them quite the way you want. but, there's good news: building a new array that contains a count of each name occurrence is just a few lines of code, but it would be useful to first define a struct to drive your List view:
in your ContentView, you'll want to have these (here, i am assuming you have some sort of
add an .onAppear() modifier to some view in the ContentView's body property
finally, loadData is where you repackage the inventoryItems into listItems. (Note: code edited a few hours after initial post, and, unfortunately, a day later, because i think i originally put the wrong dictionaty type in the code)
i think this should work. i've used a similary technique to form sections in a list. you may also now want to sort the listItems, depending on how you want them ordered (it's cheaper to sort this list that to supply a sortDescriptor int the fetchRequest). hope that helps, DMG -- |
|
hi again, 'been thinking about this overnight, and while i gave a possible answer to your programming question (which i have updated as well), perhaps you would consider redesigning the CoreData setup. instead of a single entity, perhaps called
then your list has a single fetch for ItemDescriptions and your list elements report the name and the number of instances ( it makes a little bit of sense, i think? hope that helps, DMG |
|
|
|
Hi ck Just adding onto what delaware was saying..... You can work with Core Data directly with SwiftUI. If you have no specific reason to create seperate data model to load core data entities into then you can avoid it. One good way using Core Data for your needs is as follows -
What is happening here is that you are setting up a constraint in your Core data model to say that you dont want entities saved that have the same name. In your case you dont want a seperate entity saved for everything called 'sprocket a'. By adding the context.mergePolicy you are telling Core data that if a item does have the same name then merge it with whatever entity has the same name. So all your sprocket a's for example will be saved under one entity. Now when you save each 'sprocket a' you also create a create an ItemInstance (as described by delaware) and add this to the 'sprocket a' array of ItemInstances. The end result you will have only one sprocket a entity saved with an array of ItemInstances. I have created a very quick and basic example for you to give a try - NB - I have created 2 entities in Core Data Model. One which is called Part which has an attribute 'name' and has a one to many relationship with the other entity called Details which has 2 attributes called 'serialNumber' and 'date' with a many to one relationship with Part. I have put the constraint on the Part entity for its attribute called 'name'. I have just made all attributes for both entities as Strings for ease of explanation but you can change them to suit.
Just create a dummy project, create entities like i have and paste all this and run it. See if it fits what you are after. Let me know if there are any issues. Dave |
|
Thanks Dave, this is also super helpful. However the specific goal I am trying to reach from all of this is to inlcude, along with the name in that main initial list of parts (what you included on the parts list tab item), the total count of that part from the details entity. I'm really just looking to display a single row with a total count for each part, and then link through to get the detil view. The obvious solution would be to do a second fetch on the details entity and count each group of the specific part on each itteration of the foreach loop, but to me, that sounds like it would not be the most effecient way considering my details entity could ultimetly have 100k+ entries. Is there a more efficeint way to do this or am i over thinking the problem? |
|
hi, perhaps i should have provided some code with my "been thinking about this overnight" comment. and yes, i think you are overthinking this. the main view that provides names and counts only requires a fetch of what i called
the DetailView then shows a list of the description's instances. there is no second fetching required for that view: it already has a live ItemDescription in front of it, and its (ADDED well after this was originally posted): it occurred to me that you might not know how to read the
now you can write the ForEach component of the view as
hope that helps, DMG |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.