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.
Related
I would like to create a simple app in Xcode with two UIPickerViews that reference a data set where the second UIPickerView is dependent on the first one. I want to create an app where the user can select the manufacturer of a vehicle; Chevrolet, Dodge, Ford, etc. Then, the user can select the vehicle based on the first choice. For example if "Ford" was selected in the first UIPickerView, then only Ford vehicles show up in the second - F150, Focus, Mustang etc. After selecting both values, the user can search for the average price where the prices are kept in a data set. I found many examples with one UIPickerView referencing arrays, but I want to reference a much larger data set. How would I go about doing this? I am fairly new to Xcode, but I write SAS and SQL code daily.
I am assuming you have all of records saved in the database. I did something similar with 250k+ records.
Do not fetch all of your models' full representation into memory, fetch only one property (string column needed for current picker) with a DISTINCT on it - both SQLite & CoreData allow this.
Your subsequent pickers (2nd, 3rd & so on) will automatically see less data becuase of the previous filter applied (only Ford vehicles possible options).
Rule #1 applies to all of your pickers, only the relevant field as String pulled into memory with right filters.
I had no issues at all with above approach with my dataset. Not sure how big your dataset is.
I have a single table called SPARES
The table has 3 fields, Stock_No, Store_Id, Description
In the data set there are duplicate Stock_Nos and in some cases the descriptions vary
I make one query to extract the duplicate stock Nos then tried a 2nd query based on the first but don’t know how to extract records with duplicate Stock_Nos but with different descriptions
Can anyone help?
Can it be done in one query?
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.
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.
I've been researching this for a while and I think there are a number of solutions, but I'm not sure they are that good. It may be that I've missed something :-)
I have two tables. For each record in TableA there are multiple records in TableB. I.e. a one to many relationship. I've mapped it out in core data and generated the classes. So far so good.
My table view based UI needs to look like this:
Section 1
Cell 1: Table A: field1
Cell 2: Table A: field2
-----
Section 2
Cell 1: Table B: record 1: field1
Cell 2: Table B: record 2: field1
....
When I'm in my tableView:cellForRowAtIndexPath: it's easy to handle section 1 cells because they are different fields from the single Table A record. The section 2 cells are different. Each time tableView:cellForRowAtIndexPath: is called I need to get the correct Table B record from Table A's NSSet. And thats the problem.
NSSet doco indicates that it does not garrantee the order. Which means that if I want a specific record based on an index I have several possible techniques:
Add a sort descriptor to the core data query and use the fast enumeration to get to the record I want. I don't know if this will work because we are still working with an NSSet.
Sort the NSSet using a sort descriptor after getting the data to generate a NSArray and store that in a property before starting the table load.
I'm now thinking of a third option which is to write a decorator for an NSSet which acts like a NSArray and allows me to specify a sort field and automatically tracks changes to the NSSet. Tricky but might pay off in the long run.
neither of these answers seem that great. Is there a better way to do this?
If you just want your objects, use the NSSet accessor.
If you want the object sorted, set up an NSFetchRequest and add the appropriate NSSortDescriptors.
Then, simply perform the fetch with executeFetchRequest:error:
Even better, Since this is specifically for a tableView, use NSFetchedResultsController to populate the table view. That's what it's there for.
I've ended up with a NSArray being stored in the controller and sorting the NSSet records into it.