Re sorting fetchedresultscontroller once the data is fetched - iphone

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

Related

NSFetchedResultsController predicate to eliminate duplicates of several properties

I am using an NSFetchedResultsController in my UITableViewController.
Is it possible to specify a predicate that will not retrieve items which have duplicate fields in x number of fields that I specify.
For example, I want to search all results for items but if the itemName AND itemDescription AND itemQuantity are the same, I want only one of these items.
Option 1
When the page loads do a single run through the data and keep a list of objectID that are duplicate. For duplicate object set the row height of the cell to be 0. So they are technically still there, but you can't see it. This make dealing with the NSFetchedResultsControllerDelegate calls easy because no indexPaths have changed
Option 2
If the dataset is always selected in the same way and an object that is a duplicate is always a duplicate you can set an 'isDuplicate' in the object and filter it out in the predicate. Or you can not store at all in the first place. If objects are displayed in different sets and in different way and sometime should be displayed and sometime not be displayed this is not a good solution
Option 3
If you are sorting by the same criteria that make an object duplicate (that is duplicates always appear right next to a non-duplicate) and you are NOT using sections, then you can use sectionKeyPath. SectionKeyPath groups items together into sections. Group the duplicate and non duplicate together and then display every section as a single row (use the first item in each section). The indexPaths of the fetchedResultsController will not match the indexPaths of the tableview so you have to careful to convert them.
Option 4
Instead of accessing the objects from a fetchedResultsController do a fetch and and filter the array. Then use the array to display the objects. The downside is that you don't get updates on when objects change. This can be especially problematic is objects are deleted, as accessing a managedObject that's entity was delete can lead to a crash.
I recommend option 1

How do I rearrange my UITableView with respect to alphabets or with respect to modified date

I have two table views displaying the data from a Sqlite database.
I can modify the content of the 2 tables. One displayed date and data. One displayed data only.
I.e. I can change date & time,words as well.
Now I need to arrange the content with respect to date in 1 table,
with respect to alphabets in 2nd table.
What to do?
You can use a sortDescriptor to sort the array (or fetchRequest when using Core Data) you're using. If you've setup your tableViews any other way please provide some more information.
Documentation: SortDescriptors
Once sorted you can reload your tableView: [tableView reloadData];
You must be storing your data in some data structure (like array, dictionary etc.), once you edit the date and time, Sort your array or dictionary using sort descriptors and then reload the table

Core Data Fetched Results Order

I"m working with a core data model of Employees. Each employee is assigned a building and a department. I am returning everyone in building 1 with the predicate, building == 1 and using the sectionNameKeyPath parameter to break the result set down into groups by department.
Right now I have a sort descriptor ordering the departments alphabetically, but is there a way to ignore sorting them alphabetically and organize them in my own way, like engineers first, security second so on and so forth? I'm still getting used to the terminology and there's probably something I'm overlooking.
Thanks,
Note that the sortDescriptors property of the fetched results controller is an array, so you can set multiple sort descriptors which will be used in the order you specify them (e.g. first by department.name, then by lastName, then by firstName).

Separating UITableView data into sections from one source

I have one table in my core data source which holds some articles with NSDate's. I basically want to separate the NSManagedObject into days, so you can scroll through and separate articles by date.
What is the best way to approach this? My context is queried out in descending date order, so just need to split that up into days for the UITableView sections, rather than 1 big section.
When you load the data for your table -- whether that's using NSFetchedResultsController and Core Data, or loading a .plist file into an NSDictionary object -- you can "section" it as you like.
You'll first step through the data (e.g. use the fetchedObjects property if using NSFetchedResultsController) and determine what sections you want. Since you're wanting to split on dates, you might store cutoffs represented by NSDate objetcts in an array. You would then use this array to implement the various UITableViewDataSource methods: in numberOfSectionsInTableView: you can return the count of this array, and in sectionIndexTitlesForTableView: you can return NSString representations of those stored NSDates for the section titles.
Methods like tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: are a little trickier. You'll probably want to use another "preprocessed" data structure (perhaps a two-level array of arrays, where the inner arrays contain the entity objects you fetched), and you'll want to set up as the same time as your array of NSDate objects.

Optimizing text searching using NSFetchedResultsController & Core Data

I have a tableview with a NSFetchedResultsController data source, displaying a list of names from the underlying Core Data SQLite store. I have implemented a search bar. When the first character is entered in the search bar, a fetch request in the following form is executed:
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:#"name beginswith[cd] %#", searchText];
However, when the second search character is entered, I would like to filter the fetched objects of the fetchedResultsController, rather than executing another fetch request (implementing another fetch request, similar to the 1 above, results in another trip to the store which I had hoped would not occur as the results of the second fetch would just be a subset of the first). Is there anyway to filter the fetchedResultsController so that another trip to the store is avoided?
Yes, set up a search "state" and then switch your NSTableViewDatasource to point at an array that is filtered from the -fetchedObjects returned from your NSFetcResultsController.
You can then update the filter on that array as the user types more information in and it will not go back to the store. This will even allow you to filter on the first character entered and avoid even that unnecessary trip to the store.
You could always store the results of the first fetch into an array and when text in the search bar changes, filter the contents of the array with another predicate.