0% found this document useful (0 votes)
48 views10 pages

Stanford CS193p: Developing Applications For iOS Fall 2011

This document summarizes key topics from Stanford's CS193p iOS development course in Fall 2011, including: 1. Core Data is not thread-safe and various approaches for handling this, like performBlock. 2. NSFetchedResultsController makes it easy to display Core Data in a table view by handling all the data source methods. 3. The Photomania demo app shows how to use Core Data with UIManagedDocument to store photos in entities and display them in a table view.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views10 pages

Stanford CS193p: Developing Applications For iOS Fall 2011

This document summarizes key topics from Stanford's CS193p iOS development course in Fall 2011, including: 1. Core Data is not thread-safe and various approaches for handling this, like performBlock. 2. NSFetchedResultsController makes it easy to display Core Data in a table view by handling all the data source methods. 3. The Photomania demo app shows how to use Core Data with UIManagedDocument to store photos in entities and display them in a table view.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Stanford CS193p

Developing Applications for iOS


Fall 2011

Stanford CS193p
Fall 2011
Today
Core Data Thread Safety
NSManagedObjectContext is not thread-safe.
What to do about that.

Core Data and Table View


Very common way to view data from a Core Data database
So it is made very easy with NSFetchedResultsController

Big ol’ demo


Photomania
Browse recent photos by Photographer who took them

Stanford CS193p
Fall 2011
Core Data Thread Safety
NSManagedObjectContext is not thread safe
Luckily, Core Data access is usually very fast, so doing it in the main thread is mostly fine.
Always safe to access from the thread in which it (or its UIManagedDocument) was created.
Feel free to take this approach for your homework (it’s the most straightforward).

Another Approach
[context performBlock:^{ // or performBlockAndWait:
// do stuff with context
}];
This will make sure that the code in the block happens on the context’s safe thread.
Note that this might well be the main thread, so you’re not necessarily getting “multithreaded.”

Advanced Approach
Some contexts (including Core Data ones) have a parentContext (a @property on NSMOC).
The parentContext will almost always have its own thread, so you can performBlock: on it.
But it is a different context, so you’ll have to save and then refetch to see the changes.
Stanford CS193p
Fall 2011
Core Data and Table View
NSFetchedResultsController
Hooks an NSFetchRequest up to a UITableViewController
NSFetchedResultsController can answer all of the UITableViewDataSource protocol’s questions!

For example ...


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

Stanford CS193p
Fall 2011
NSFetchedResultsController
Very important method ... objectAtIndexPath:
- (NSManagedObject *)objectAtIndexPath:(NSIndexPath *)indexPath;

Here’s how you would use it in, for example, tableView:cellForRowAtIndexPath:


- (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;
}

Stanford CS193p
Fall 2011
NSFetchedResultsController
How do you create an NSFetchedResultsController?
Just need the NSFetchRequest to drive it (and a NSManagedObjectContext to fetch from).
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”; // careful!
Be sure that any cacheName you use is always associated with exactly the same request.
It’s okay to specify nil for the cacheName (no cacheing of fetch results in that case).
It is critical that the sortDescriptor matches up with the keyThatSaysWhichSection...
The results must sort such that all objects in the first section come first, second second, etc.

Stanford CS193p
Fall 2011
NSFetchedResultsController
It also “watches” changes in Core Data and auto-updates table
Uses a key-value observing mechanism.
When it notices a change, it sends message like this to its delegate ...
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
// here you are supposed call appropriate UITableView methods to update rows
// but don’t worry, we’re going to make it easy on you ...
}

Stanford CS193p
Fall 2011
CoreDataTableViewController
NSFetchedResultsController’s doc shows how to do all this
In fact, you’re supposed to copy/paste the code from the doc into your table view subclass.
But that’s all a bit of a pain, so ...

Enter CoreDataTableViewController!
We’ve copy/pasted the code from NSFetchedResultsController into a subclass of UITVC for you!

How does CoreDataTableViewController work?


It’s just a UITableViewController that adds an NSFetchedResultsController as a @property.
Whenever you set it, it will immediately start using it to fill the contents of its UITableView.

Easy to use
Download it along with your homework assignment.
Just subclass it and override the methods that load up cells and/or react to rows being selected
(you’ll use the NSFetchedResultsController method objectAtIndexPath: mentioned earlier).
Then just set the fetchedResultsController @property and watch it go!

Stanford CS193p
Fall 2011
Demo
Photomania
Gets recent photos from Flickr.
Shows a list of photographers who took all the photos.
Select a photographer -> shows a list of all the photos that photographer took.
Core Data Entities: Photographer and Photo.

Watch for ...


How we open/create a UIManagedDocument to hold our Core Data database.
How we define our database schema graphically in Xcode.
How we create NSManagedObject subclasses and then add categories to them.
Especially how we use categories to create “factory” methods to create/initialize database objects.
How we use CoreDataTableViewController to hook the table views up to the database.

Stanford CS193p
Fall 2011
Coming Up
Homework
Virtual Vacation
Let the user create a “virtual” vacation and then go on a vacation any time they want!
Builds on your Fast Map Places application from last week

Friday Section
Mike Ghaffary
Director of Business Development at Yelp!
Also co-founder of BarMax, the most expensive iPhone/iPad app on the AppStore
Topic: Building Apps that People Want
Understanding Market Opportunity
Building a Prototype
Financing a Company or Team
Getting User Feedback
Distribution through the AppStore

Stanford CS193p
Fall 2011

You might also like