Archive for » March, 2010 «

controllerDidChangeContent is your best friend when using CoreData

If you are using CoreData, most likely you need to be informed of changes to your table when you have either uploaded or deleted data.

The best way is to use this callback to update the table.  Sample code below:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.

//    reload the list of region
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:@"All"];
NSEnumerator *region = [[fetchedResultsController sections] objectEnumerator];
id <NSFetchedResultsSectionInfo> sectionInfo;
while (sectionInfo = [region nextObject]) {
[array addObject:[sectionInfo name]];
}
wineRegionList = array;
wineRegionIndex = 0;

//    reset region to default value
regionVC.wineRegionList = wineRegionList;
regionVC.wineRegionIndex = wineRegionIndex;
[regionVC.tableView reloadData];
[array release];
}

The GodFather of Energy Efficiency

A group of prominent scientists has proposed that “the Rosenfeld,” a unit for electricity savings, be named after Bay Area physicist Arthur Rosenfeld.

The 83-year-old Berkeley resident is known as the “godfather” of energy efficiency and served on the California Energy Commission from 2000 to January this year.

“The Rosenfeld” would be defined as electricity savings of 3 billion kilowatt-hours per year, the amount needed to replace the annual generation of a 500-megawatt coal-fired power plant. Rosenfeld often uses the example of power plants when explaining the benefits of energy efficiency to students.

A lazy man’s guide to cutting energy costs

Not sure why people here in Singapore aren’t that crazy about measuring energy usage.  People here prefer to spend more on air-conditioner than considering other factors.

Singaporeans also like to complain about the tariffs hike, but do near to nothing when trying to conserve energy use.

Not like in places where they place sensors and controllers within the house to measure peak, off peak and usage patterns to reduce energy cost.

Best way is to follow A lazy’s man guide to cutting energy costs.

UISearchBar together with TableView working with Core Data

This is interesting.  Got some examples of UISearchBar and the like of capturing the text input and resignFirstResponder when finished with search.

Core Data has good examples of tableView and how to retrieve and display data.

However, not much info is available, even from books, blogs and others on how to use UISearch with an existing TableView with data from Core Data.

Seems like you are on your own when using these 3 related APIs. Together, these 3 APIs can give you the best way to view and search data fetched  using Core Data.

Core Data gives you good memory management when using a large database.  Data is faulted, read from the database for viewable cells.  No need to create a huge array to hold the data.

One way to differentiate which table to display; i.e. normal or searched can be dependent on

// when we start/end showing the search UI
- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
useSearch = YES;
}

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
useSearch = NO;
}

You can use this flag to determine which table to view for your UITableViewDelegate methods.

// called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
searchBar.text = nil;
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:YES animated:YES];

//    reset the fetch search controller
fetchedSearchResultsController = nil;
}

You also need to reset the fetchedSearchResultsController at the end of search.  Have to make sure that search results are refreshed upon new search.

Category: Core Data  One Comment