Lecture #14: Core Data Demo

Please note, this blog entry is from a previous course. You might want to check out the current one.

Lecture fourteen is named “14. Core Data Demo (November 10, 2011)” and can be found at iTunes. Its slides are available at Stanford.

The theoretical part of this lecture is quite short providing an overview about Core Data thread safety and its usability for table views.

NSManagedObjectContext is not thread safe, thus it can only be accessed in the thread that created it. Because access is usually very fast, using the main thread is mostly file. Another approach is to use

[contextperformBlock:^{ // orperformBlockAndWait:
    // access database
}];

which make sure to perform the operation in the context’s thread. Yet another way would be to use a parentContext to performBlock on it. But because this would be a different context, it is necessary to save and refetch to see changes.

NSFetchedResultsController hooks an NSFetchRequest up to a UITableViewController allowing to display the content of an database automatically in a table view. Once set up it provides methods to be used for the table view controller, e.g.:

- (NSUInteger)numberOfSectionsInTableView:(UITableView *)sender
{
    return [[self.fetchedResultsController sections] count];
}

- (NSUInteger)tableView:(UITableView *)sender 
  numberOfRowsInSection:(NSUInteger)section
{
    return [[[self.fetchedResultsController sections] 
        objectAtIndex:section] numberOfObjects];
}

- (NSManagedObject *)objectAtIndexPath:(NSIndexPath *)indexPath;

- (UITableViewCell *)tableView:(UITableView *)sender
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = ...; 
    NSManagedObject *managedObject =
        [self.fetchedResultsController objectAtIndexPath:indexPath];
    // load up the cell based on the properties of the managedObject
    // of course, if you had a custom subclass, you’d be using dot notation to get them
    return cell;
}

An NSFetchedResultsController is created by using an NSFetchRequest with its NSManagedObjectContext, e.g.:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@“Photo”]; 
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@“title” ...]; 
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
request.predicate = [NSPredicate predicateWithFormat:@“whoTook.name = %@”, photogName];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
    initWithFetchRequest:(NSFetchRequest *)request
    managedObjectContext:(NSManagedObjectContext *)context
      sectionNameKeyPath:(NSString *)keyThatSaysWhichSectionEachManagedObjectIsIn 
               cacheName:@“MyPhotoCache”;

It also takes care of changes in the database and updates the table automatically by using:

- (void)controller:(NSFetchedResultsController *)controller
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    // update rows
}

The demo of this lecture shows how to create a Core Database and how to use it populate tables. It uses an adapted version of the FlickrFetcher and a generic Core-Data table view controller which both are available at Stanford. The complete code is also available directly at Stanford or at github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *