Home >android apps market for tablests > Core Data Navigation-Based Application
Core Data Navigation-Based Application
Posted on Monday, July 6, 2009 by android apps market for tablests
There's a problem with the Core Data Navigation-Based Application Template in 3.0. In the current 3.1 beta, the problem has been partially (but not fully) resolved (can't give details because of the NDA, sorry), but hopefully it will be fixed in the final version of 3.1.
If you use the template in 3.0 and run the sample application, the application will crash if you try to delete the last or the only row in the application.
The problem is here:
What happens is that the object is deleted from the context here:
it causes the Fetched Results Controller to removes the object from its resultset and the corresponding table. So, when, a few lines later, when it attempts to delete the row from the table:
The row it's trying to delete is no longer there.
The solution to this, however, is non-obvious. In fact, I didn't figure this one out myself. In the 3.0 template, the following method is commented out:
The first thing you should do is uncomment this method. You want to be getting change notifications from your fetched results controller. However, if we leave it as-is, we'll start getting that same problem when we delete every row, instead of just when we delete the last or only row. To resolve this, you need to add one line of code to the uncommented method (it's in bold):
Once you've made that change, you should be good to go. In fact, you might want to go and change the code in the project template by making the change to the file called RootViewController.m at the following location (assuming you've installed the dev tools in the default location:
Thanks to iPhone Developer Rod Brown of TheBarcodeProject for pointing me in the right direction on this issue!
If you use the template in 3.0 and run the sample application, the application will crash if you try to delete the last or the only row in the application.
The problem is here:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Delete the managed object for the given index path
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error;
if (![context save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}What happens is that the object is deleted from the context here:
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];it causes the Fetched Results Controller to removes the object from its resultset and the corresponding table. So, when, a few lines later, when it attempts to delete the row from the table:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];The row it's trying to delete is no longer there.
The solution to this, however, is non-obvious. In fact, I didn't figure this one out myself. In the 3.0 template, the following method is commented out:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// In the simplest, most efficient, case, reload the table view.
[self.tableView reloadData];
}The first thing you should do is uncomment this method. You want to be getting change notifications from your fetched results controller. However, if we leave it as-is, we'll start getting that same problem when we delete every row, instead of just when we delete the last or only row. To resolve this, you need to add one line of code to the uncommented method (it's in bold):
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// In the simplest, most efficient, case, reload the table view.
if (!self.tableView.editing)
[self.tableView reloadData];
}Once you've made that change, you should be good to go. In fact, you might want to go and change the code in the project template by making the change to the file called RootViewController.m at the following location (assuming you've installed the dev tools in the default location:
/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application/Navigation-based Application/Navigation-based Core Data Application/Classes
Thanks to iPhone Developer Rod Brown of TheBarcodeProject for pointing me in the right direction on this issue!
Powered by Blogger.
Blog Archive
-
▼
2009
(496)
-
▼
July
(61)
- We're Listening
- We're Listening
- The Iterative Web App: Links Got Shorter and Smarter
- The Iterative Web App: Links Got Shorter and Smarter
- DP aka Dataran Pahlawan Malacca.
- The Most Reliable
- Google Latitude. Now for iPhone.
- Google Latitude. Now for iPhone.
- Interesting Contrast
- Waving the Red Flag
- Microsoft's Ads Redux
- Layers of fun in Google Maps for mobile 3.2
- Layers of fun in Google Maps for mobile 3.2
- August 14 Workshop Update
- To VBO or Not to VBO...
- Improved Blender Export
- Refactoring Nav from Chapter 9
- Voyager Class Mixmaster
- Fixed & Improved Blender Export Script
- How MJ Died..
- A Little Ego Boost for a Friday
- WWDC 2009 Videos Available for Purchase
- Beginning iPhone 3 Development is Available
- Gah! Up is Down! Right is Wrong! Make it Stop!
- First Post
- Search with My Location for iPhone 3.0. And All t...
- Search with My Location for iPhone 3.0. And All t...
- Browse a collection of experts' favorite places us...
- Browse a collection of experts' favorite places us...
- New Device Rules
- Updating Project Hint
- Google Voice for Android and BlackBerry
- Google Voice for Android and BlackBerry
- Detail Editor Update: To Many Relationships
- Generic Detail Editor Update
- August 14 Workshop
- A Category on NSDate
- unusual business ideas that work Gadget
- Core Data Default Dates in Data Model
- A Kick-Ass Objective-C Programmer is a Kick-Ass C ...
- TinyPlayground Gadget
- Google Product Search for mobile now available for...
- Google Product Search for mobile now available for...
- Travelzoo UK Deals
- In Search of a Better Way: Editable Detail Views
- The Perils of Helping Others
- Core Plot
- More on Teaching
- An IPhone App Setback
- Oh, Good, Here Comes the Justice Department
- Windows Mobile: World Domination is In Sight!
- Core Data Navigation-Based Application
- iPhone Simulator Application Creator
- Wireless Carriers the Great Train Robbers of this ...
- An Exercise in Blatant Bias
- Core Data - Inserting a New Managed Object
- Google Search results optimized for feature phones...
- Google Search results optimized for feature phones...
- What, Me Teach?
- Mint Apps Promo Day is Today!
- Core Data - Determining if a Managed Object is New
-
▼
July
(61)