Tableview with sections managed by fetched results controller - iphone

I'm quite new to CoreData and need help with one issue. I have tableview which simply lists cities stored in SQLite db. City is defined by "cityId" and "name" attributes.
Data are fetched using NSFetchedResultsController and everything works fine except that I can't figure out how to make FetchedResultsController to group cities by first letter of city names.
When I supply "name" for sectionNameKeyPath argument of initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName: method, it creates section for each city, which is obviously something I don't want. I can't figure out how to create an expression which would make it work in way I need.
thanks for any tips and/or advices on this
Matthes

Related

swift : get the closest date stored in the core data

I have two data in core data.
Entity Name = "Contact", Attribute Name = "date"(selected datePicker.date), "content"(textFeild.text)
It is that sort of information in a table view, was a success. I want to show only one content in the another view controller. The data of the closest time to the current time.
I was asked to help with the following code.
var stores: Contact?
if ((stores?.date!.timeIntervalsince1970 ?? 0) > NSDate().timeIntervalSince1970) {
labelName.text = stores?.content!
}
But the label has not been any indication on. And no error... What's the problem?
As you mentioned using Core Data, the right direction is to Fetch this objects (the contacts). Then you will have an Array of NSManagedObjects. If the fecth is successful you iterate them in a ´for in´ loop.
Inside this loop use ´.valueForKey´ to extract the dates and apply the rules you have created, returning the NsManagedObject or Atribute you want.

Re sorting fetchedresultscontroller once the data is fetched

I have a situation where i need to sort the data using multiple sort descriptors. I want to sort the data initially with two components which are numbers in descending order. Then I should show the data on the table view sorted with name component and use the sectionNameKeyPath to get the sections.
I want the data should be sorted out first with the two specified components and then the result with name. Is this possible using NSfetchedResultsController. I am using this fetchedresultscontroller in data source for tableview. Is there anyway to re sort the fetchedresultscontroller?
I can sort it by taking an array and using sortdescriptor on that, but I want fetchedresultscontroller to show the section details and etc.
Is there any way?
I don't really sure if I have understood your answer.
But there is a property called sortDescriptors in NSFetchRequest which you may use it to sort.
And it's an array so you can supply multi NSSortDescriptor objects
And if you want to resort it.
I know two ways:
Store the result in an array like you said and resort it.
Change the sortDescriptors and re performFetch of the FRC

How to give relation among group of NSMutableArray?

my application is world guide. there is a 4 tableview and 4 mapview.
Continent
Country
City
Place
I have taken NSMutableArray for each above item and allocate them in view did load . when Asia is selected from the Continent table then only Asian countries should display from the country array. if country is selected then only city of respective country should be display in city table view from the city array like vice for place. it's like a tree. it is too lengthy to work with an array. Please guide me if another way is exist that make this task easy.
I think the better way to implement should be:
Use NSMutableArray only to store the top level Object.
Use NSMutableDictionary to store the mid & lower level objects.
Store the NSMutableDictionary in your NSMutableArray at each index of your top level Object.
Put the key as the Continent name in top NSMutableDictionary & value as country name & follow the same for next level.
In this case, you have to maintain only a single NSMutableArray to work. No need to handle 4-5 different arrays.
Hope it works for you.
To handle the data or categorize the data with the way you have the data about all four objects is quite complex because there is no key value pair by which you can identify that which country comes under which continent and which city comes under which country and so on.
So what you have to do is, use web service by which you can fetch the categorized data.
Here is link of web service by which you can simply hit the URL and catch the XML as the response.
URL : http://ws.geonames.org/countryInfo?
Above URL gives you the XML as the response. Like this :
<geonames>
<country>
<countryCode>AD</countryCode>
<countryName>Andorra</countryName>
<isoNumeric>020</isoNumeric>
<isoAlpha3>AND</isoAlpha3>
<fipsCode>AN</fipsCode>
<continent>EU</continent>
<continentName>Europe</continentName>
<capital>Andorra la Vella</capital>
<areaInSqKm>468.0</areaInSqKm>
<population>84000</population>
<currencyCode>EUR</currencyCode>
<languages>ca</languages>
<geonameId>3041565</geonameId>
<west>1.4071867141112762</west>
<north>42.65604389629997</north>
<east>1.7865427778319827</east>
<south>42.42849259876837</south>
</country>
.
.
.
and so on...
or if you want JSON format. take the following URL :
URL api.geonames.org/countryInfoJSON?username=demo&formatted=true
So now you are just few steps away from your solution.
Now Do the XML parsing and filter the data according to your requirement.
or if you want to have the detail knowledge about this web service
just visit : http://geonames.r-forge.r-project.org/
Hope it works for you.
I suppose you have all the data about Continent, Country, City and Place. In this situation the way of connecting all 4 arrays is totally depends upon the format of data you have for displaying on TableViews.
Whether it is a array of string or array of dictionary for each item or JSON or XML.
Please tell me the data format. After that i'll try my best.

How to iterate through an NSArray of objects and create a new NSArray containing a subset of the original?

I am working on a basic iPhone app that pulls restaurant objects from a SQLite table, and displays them for the user in a table. My app is navigation based. I have no problems pulling the data from the SQLite database, and then sorting them. That I am able to do no problem. Each restaurant object has various properties, like "name", "address", "category" (i.e. the type of restaurant like greek, italian), and others. Now, after retrieving the records from the database, I am able to successfully put them into an NSMutableArray, and then sort them into an NSArray.
My question is, using this NSArray (or the original NSMutableArray), I'd like to create another array, with the first index containing "All", and then each remaining index contain the unique name of the category of restaurant from the original list, sorted in alphabetical order, and then present this as a table to the user on the next screen. From this table, if the user selects "All", the user will be taken to another screen displaying another table where they can see all the restaurants, regardless of genre.
If however, the user selects a particular category from the available list, the user will be taken to a screen that displays a table composed of only those restaurants of that type (e.g. only Italian restaurants, only Chinese restaurants, only Thai restaurants). How would I go about doing this? Any ideas or suggestions?
One possible algorithm outline:
Iterate over your NSMutableArray (e.g. for(item in array)) inserting the category property of each item into an NSMutableSet (or an NSCountedSet if you'd also like the number of times each category occurs)
Use the NSSet sortedArrayUsingDescriptors: method to obtain a sorted array of the categories.
Add the All entry in whatever way suits

How do you create an indexed table view based on last name

If you could find a tutorial for an indexed table view based on last name like the "Contacts" app, that would be great. I've search all around but can't find one on last names.
I guess you store your contacts using core data. You probably have a entity "Person" with attributes first name, last name, address etc.
You should make a subclass of NSManagedObject specifically for the entity "Person". In this class, you add a method that returns only the first letter of the last name. If you have a NSFetchedResultsController you can apply the path name for the sections. There you just write the name of exactly that method that returns the first letter. The table view should then be sectioned by the last name's first letter like in contacts.
Now, you should implement the method
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
You return a array that contains all the index titles which will appear on the right. Let's say if your contacts are "Steve Jobs" and "Jony Ive", there would be two sections and two index titles "I" and "J". You don't have to put all letters of the whole alphabet in the array, just those that you really need.