NSFetchRequestController use sectioning parameter as part of title for section - ios

I have a Core Data application with two entities. One is the Order Entity and the other is the Sales Order Numberentity. The Order entity contains an attribute that basically represents which sales order number it is a part of. I am using an NSFetchedResultsController to try and group this list of orders by sales order number. This is done using the sectionNameKeyPath parameter of the init method of NSFetchedResultsController. However, I want to use attributes of the Sales Order Number entity in the header of the section and this is proving annoying. I could get the first Order in the section and then backtrack from there using the relationship to get the Sales Order Number that it belongs to and then pull attributes from there, but is there a better way to accomplish what I am trying to do?
Sorry if this is unclear.

Related

How to model a "Character-Inventory-Item" schema in Core Data

Essentially I am trying to model a character holding a backpack with items from a pre-determined list. So far I have come up with this.
My main issue is in understanding how core data handles Arrays/Lists etc. From what I have read that is determined by the relationship, a simple character - item relationship is what I first came up with but I wanted to be able to add custom descriptions per item selected from the pre-determined items (which can be added to by the user at runtime). Each character would have only one "backpack" with a list of items with custom descriptions and custom "amounts" or count.
That backpack could theoretically have 2 of the same items but with different descriptions hence having a count of 2 for the one item wouldn't always be appropriate.
Also, there is the option for multiple character profiles, so therefore the items could belong to multiple different backpacks, but again with difference in description/count etc.
So I guess my main issue is understanding how Core Data handles lists. And how i could properly address this issue to allow for a "character-backpack-item" relationship.
Thanks!
Short and quick clean-up:
1: Add the properties from item to your BackPackItem
2: Remove the itementity
That backpack could theoretically have 2 of the same items but with
different descriptions
3: Add a property backpackItemID to your BackPackItem and assign a unique ID to it each time you create a BackPackItem entity. That way you can have multiple items with same information but with different ID's. (Not needed , but keeps things more clean in my opinion, do as you wish here)
Finally:
Now all you need to do when you fetch is to fetch the Character entity by it's name for example. And in your NSFetchRequest you add the BackPackItem (associatedBackPackItems as the relation is named) as relationshipKeyPathsForPrefetching, and all the associatedBackPackItems will be fetched for you automatically.
Now let's say you have fetched a Character , and you want to access its BackPackItem --> character.associatedBackpackItems gives you all the items connected to that character.

How to save multiple text inputs to Core Data with multiple rows in sections?

Okay so I'm making a app for fun to get a hang of things. This app is supposed to allow a user to input the number of times he or she has been paid from a client and which account/card number he or she used for the payment. The way I have this set up in CoreData is I have an entity for Client which contains:
details
name
entryId
sectionId
numberOfPayments
details is another cell type that I lets out in the picture that allows the user to input details about the client. I also have an entity for Payments that acts as a many to one relationship to Client that holds the amount and account number given that there can be many.
What I have trouble with is accessing the tableview to save all the data when the done button is hit. I was thinking about a nested for loop that would go through each section and for each row it will save the data accordingly like for Rob through an if statement it will know the first row is a part of the Client entity and every row after would be apart of the Payments entity as relationships. How would I access these individual cells? Is it possible to have this much flexibility with TableView or should I take a different approach.

iOS retrieving and ordering Core Data entities with relationships

I recently asked this question about how best to retrieve and display in a tableview the titles my FRC is using for section headers.
Following a line of research suggested by #Mike Pollard in the second answer to my question, I ran across this question and was immediately struck by the similarity to my situation, and by the 4th answer, posted by #aroth.
Aroth's approach certainly appears sound, and I've created a new Category entity. Specifically, Category has a to-many relationship with Item, and Item has a to-one relationship with Category. However I'm having trouble understanding one aspect implicit in his proposed solution, and, more fundamentally, in this relationship:
In my case, both Category(s) and Item(s)--"Item" is called "ListActivity" in my case, but "Item" will do for illustration purposes-- will be named via two corresponding user input fields, which seems like it could result in multiple entries of the same name in the Category list.
My question:
How can I ensure that when I fetch a list of Categories that I get a singular instance of each category, i.e., one category per row in the tableview, with no repeats? Will Core Data automatically assign each new incoming Item to a singular instance of the appropriate Category via the relationship? Or will it somehow test for and winnow the list down to one entry per Category name upon receiving the fetch request? Or must the filtering be done with a predicate in the fetch request?
Thanks!
Core Data will do what you tell it to. This sounds like an issue related to you creating content in your data store rather than an issue with the FRC and table view. It's your responsibility to search for and reuse existing objects rather than creating duplicates and adding them to the store - indeed, only you (your code) knows what constitutes a duplicate.
So, basically, as you create new items, use a fetch request and predicate to find the suitable existing category (or suggest categories based on partially entered names). Then, either connect to the existing category or create a new one.

Relational Database Design (E-Commence) - Core Data

In my e-commence app (for café/restaurants) I currently have the following database structure.
The cart is the shopping cart, in which you can add products, a temporary place before the products/an order is sent to the server. The ProductCart is a line item, many products (could be the same) with the different quantities, sizes, frying levels etc. When a order is sent, the cart is cleared and the products in the cart is transfered to the ProductOrder entity (an Order).
I now want to extend this further, with the ability of the products having ingredients and this is where it gets tricky and too complex for my head and database skills :-). As well as the (same) products can have different sizes and frying levels (hence the line item) a product should have the ability to have many different ingredients (add ons) for example a pizza, where you could choose the topping. This is what I have tried so far:
But I am not sure if this is the right structure or way to do it?
This is my suggestion.
Remove ProductOrder and Order entities. They are the same as ProductCart and Cart.
Now ProductCart should have an attribute like synchronized that is 1 or 0 based if it has been sent to server or not.
Through this you should simplify a lot your model. About Ingredient… entities they seem ok to me.
There is something fundamental you have not grasped about Core Data. Your ProductOrder entity is essentially a join table. This is completely unnecessary if you are not tracking additional attributes in this table.
Instead, you should have a many-to-many relationship between Order and Product.
It might seem that ProductCart satisfies my condition above that in this case a join table makes sense. But no - you should simply add the orders to your cart and track all the information in the Order entity.

Hide Duplicate Rows in Core Data Driven UITableView

I have a Core Data entity that has two name attributes and several other numeric attributes. The sort is on the numeric attributes first and last on the name attributes. Data is coming from multiple sources and so it is possible to get duplicate rows where the names are switched in order. i.e. row 1 has names Bill and Ed, where row 2 has names Ed and Bill. The rows are identical, except for the fact that the order of the names is switched.
I was hoping that there might be a way to 'hide' the duplicate rows in the table view, but since the row count is coming from the fetchedresults controller, I can't see how that would work.
Any suggestions on how to proceed?
Jim
I think the best approach is to create an array containing all the data you actually want to display. You can assign your first source to the array and for the other ones you first verify that the item doesn't exist before adding it. If you know how the duplicates can be presented then you can write the necessary conditions that need to be passed before adding it to the array.

Resources