MobileCity:Core Data

593 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
593
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MobileCity:Core Data

  1. 1. Mobile City: Core Data TaskList
  2. 2. About Me● Allan Davis – Lead Developer of Mobile Technology for DealerMatch – Cajun.code@gmail.com – @cajun_code
  3. 3. Session Agenda● Introduction(Tip Calculator)● Overview of Objective C(TicTacToe)● UI UX (TrackerPoker)● CoreData (MyBloodyTaskList)● Networking and web-services (TrackerPokerLive)● Debugging, Testing, Tuning and Distribution● Game Development with Cocos2d (Oni Attack)
  4. 4. Class Agenda● What is CoreData● Create the Data Model● NSManagedObjectContext● UIManagedDocument● AppDelegate● Generate Object Models● Create● Read● Update● Delete● NSFetchedResultsController
  5. 5. What is Core Data● Object Relational Management tool.● How does it work? – Create a visual mapping of database to objects – Create query for objects using object-oriented API – Access the “columns in the database table” using properties on the objects
  6. 6. Creating the Data Model
  7. 7. Create Model Visually
  8. 8. NSManagedObjectContext● Session Management object where all activity turns.● Two ways to create one. – Create a UIManagedDocument and ask for the context – Property off the AppDelegate if you selected “Use Core Data” when you created the project
  9. 9. UIManagedDocument● Create Property for UIManagedDocument- (void)useDocument{ if (![[NSFileManager defaultManager] fileExistsAtPath:[self.photoDatabase.fileURL path]]) { // does not exist on disk, so create it [self.photoDatabase saveToURL:self.photoDatabase.fileURLforSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { [self setupFetchedResultsController]; [self fetchFlickrDataIntoDocument:self.photoDatabase]; }]; } else if (self.photoDatabase.documentState == UIDocumentStateClosed) { // exists on disk, but we need to open it [self.photoDatabase openWithCompletionHandler:^(BOOL success) { [self setupFetchedResultsController]; }]; } else if (self.photoDatabase.documentState == UIDocumentStateNormal) { // already open and ready to use [self setupFetchedResultsController]; }}
  10. 10. AppDelegate● Generated by Xcode● Need to push context down to down to ViewControllerUINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;UIViewController *controller = navigationController.topViewController;[controller performSelector:@selector(setManagedObjectContext:) withObject:self.managedObjectContext];
  11. 11. Generate Classes from Model
  12. 12. CreateTrim *trim = [NSEntityDescription insertNewObjectForEntityForName:@"Trim" inManagedObjectContext:context];
  13. 13. ReadNSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:context];[fetchRequest setEntity:entity];fetchRequest.predicate = [NSPredicate predicateWithFormat:@"category.name = %@", category.name];fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];[context executeFetchRequest:fetchRequest error:&error];
  14. 14. Update● Make changes to property● Call save on the ManagedObjectContext
  15. 15. Delete● Call delete on the ManagedObjectContext and pass in the object to delete
  16. 16. NSFetchRequestController● Adds functionality to monitor a FetchRequest to update a table view.
  17. 17. TaskList● Simple Task List● Sorted by Category
  18. 18. Resources● Stanford CS 193P – Lecture 13 and 14 are Core Data – http://www.stanford.edu/class/cs193p/cgi-bin/drupal/● Class Links – Videos: http://vimeo.com/channels/mobilecity – Slides: http://www.slideshare.net/javaalley/ – Code: https://github.com/organizations/AtlantaMobileDevGroup

×