I’m very new to iOS development and am having an issue. I have a plist that contains a lot of information for a bunch of different animals. The plist is an array of dictionaries, and each dictionary is an animal. Each dictionary has a key called “type”, which is the type of animal (cat, dog, bird, etc). I want my application to first display a table view that lists all of the possible types. When the user selects the type, they will then go to another table view that lists all of the animals of that selected type. They can then select that specific animal and another view will be displayed that has all the detailed information about that specific animal.
I suppose the best way to approach this is that when a user selects the “type” of animal, a new plist will be generated containing the directory entries from the original plist that have a “type” key that matches what the user selects. How can I implement this? Would this logic be in the viewDidLoad method for the second table view? Once the plist is created that contains only the animals of interest, the cellForRowAtIndexPath method would display the list of animals?
Thanks for any assistance!
David
First: You shouldn't generate any new plists in this situation... If you want to store a list of dictionaries based on a query of your data, it should go into an NSMutableDictionary/NSDictionary. Before creating each table, you should construct one of these dictionaries with the filtered data, and pass it to the table as the data source.
Second: I'd try using a database instead of a plist to make your queries a little easier to manage.
Third: Normalize your data as much as possible, hence, have a database table (or plist if you go that route) of "animal types," and a db table/plist of "animals" ... then reference the animal's type by id, instead of by name. If you change that name in the future, you'll have a hell of a time trying to find every occurrence. In addition, it's less buggy that way; one accidental misspelling will leave you wondering why one animal is causing an error and the others aren't.
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.
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 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.
I'll try to be short and clear with this question.
We have an asp.net mvc app that uses entity framework 4.
Our business model is relatively straightforward:
We have an object (which corresponds to a table) called Photo(s).
That photos table has a handful of columns that match up to properties on the object.
Description,Title,Date etc.
It also has a number columns that reference foreign keys for other tables:
AuthorId,LicenseId etc...
The author and license tables are complex in their own right, with multiple fields (Title,Summary,Date etc.)
I have multiple clients using this application to view their photos. I would like each client to dictate what fields they see when viewing the photos, as well as what fields they see when editing those fields.
My thought is to have tables setup saying client-a should see Field1,Field2 and Field3 when viewing their photos - and client-b should see Field1,Field4 and Field5. But some of these fields are not simply columns in the main photos table, they may be fields in a child table. so Field1 might be: Table.Photos.Title -> which corresponds to an object as: Objects.Photo.title...
but Field3 might be: Table.Licenses.LicenseSummary -> which corresponds to an object as: Objects.Photo.License.LicenseSummary
I'm trying to figure out the methodology that we would use to have a very data driven environment so in the DB I can say, display this object/property (for viewing or editing) and then it would know how to map to whatever table it needs to pull that information. also, during editing... give it some way to pull a list of available values if it is that type of property, and not just a text field.
I'm looking for an example of what this might involve, our model is actually more complex than this, but this is just an idea of what we are trying to accomplish. I don't know if what I'm trying to do is normal, perhaps it involves reflection? This is a new area for me.
If the clients are defining their own custom fields, I would simply give them a Key/Value pairs table.
PhotoID FK
Key string
Value string
Display bool
Note that this essentially amounts to EAV, which comes with its own set of difficulties.
If it's just about permissions on existing fields, you need to capture that information:
PhotoID FK
ClientID FK
FieldName string
Display Bool
You can use this information to inhibit the display of fields in the View. The easiest way to do that would be to use a loop in the View itself, writing the field to the output only if Display is set to true.
I have a scaffolded Grails application with two domains, Person and Course. Person belongs to Course, and Course hasMany Persons. I have modified show.gsp for Course to list all of the Persons associated with the selected Course.
To achieve this, Course.groovy contains the following line:
List persons = new ArrayList()
And, as a result, the "person" database table contains a persons_idx field. I frequently will be adding new data to the "person" table outside of my Grails application, from an external website.
When INSERTing new data, how to I figure out what to set persons_idx as?
I had originally used a SortedSet instead of an ArrayList for persons, since I care about sorting. But since I am sorting on Person.lastName, and there will always be multiple people with the same last name, then the list will exclude those persons who have the same last names as others. I wish there was another way...
Thanks.
Having two applications manipulate the same Database is a thing to avoid, when possible. Can your 2nd application instead call an action on the controlling app to add a Person to the Course with parameters passed to specify each? That way, only one app is writing to the DB, reducing caching, index, and sequence headaches.
You also state that Person belongsTo Course... so you create a new Person for "Bob Jenkins" for each course that he's in? This seems excessive. You should probably look into a ManyToMany for this.
Without moving to a service, unfortunately, you'd want to change the indices on some if not many of the rows for the children of the Course you're trying to add a Person to, as that index is the sorted index for all the Persons in the Course.
I would suggest going back to a "Set", and do your sorting in the app. Your other question about sorting already told you not to override compareTo to just check the last name. If I were you, I'd forget about overriding compareTo at all (except to check IDs, if you want), and just use the sort() method, passing in a closure that correctly sorts the objects.