Lecture 12_0

Lecture 12_0 - CS193p Spring 2010 Monday, May 10, 2010...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS193p Spring 2010 Monday, May 10, 2010 Todays Topics Core Data continued Demo NSFetchedResultsController CoreDataTableViewController NSThread NSOperation NSLock NSNotifcationCenter UIActivityIndicator Monday, May 10, 2010 Core Data Now you can create an object in the database via NSEntityDescription s insertNewObjectForEntityForName:inManagedObjectContext: And know how to set/get properties on an object via the NSManagedObject s valueForKey: and setValue:forKey: methods or via @property s if you create a subclass of NSManagedObject So now you need to be able to fetch objects You do this by asking the NSManagedObjectContext to execute an NSFetchRequest . 4 things involved in creating an NSFetchRequest 1. NSEntityDescription of which entity to fetch (required) 2. NSPredicate to specify which ones of that entity to fetch (optional) 3. NSSortDescriptor s to specify the order of the objects (optional) 4. Batch size (how big of groups to fetch results in) (optional) Monday, May 10, 2010 Core Data Creating an NSFetchRequest NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = [NSEntityDescription entityForName:@Photo inManagedObjectContext:context]; request.predicate = ...; request.sortDescriptors = [NSArray arrayWithObject:sortDesc]; request.fetchBatchSize = 20; // depends on object size NSSortDescriptor NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@thumbnailURL // string property ascending:YES selector:@selector(localizedCompare:)]; Another selector option is localizedCaseInsensitiveCompare: . This is just a method sent to each object with another of the returned objects as an argument and returns an NSComparisonResult (>, <, ==). There is another initializer that does not take the selector ( compare: ). If you specify multiple of them, they are applied in order. Monday, May 10, 2010 Core Data NSPredicate Created much like a formatted NSString NSString *term = ...; // something in a thumbnailURL you want to search for NSPredicate *predicate = [NSPredicate predicateWithFormat:@thumbnailURL contains %@, term]; Check out documentation for all the options @uniqueId == %@, (NSString *)uniqueId @%@ in tags, (NSManagedObject *)tag // search in a set (to many relationship) @viewed > %@, (NSDate *)[NSDate dateWithTimeIntervalSinceNow:-60*60] @name contains[c] %@, (NSString *)searchSubstring // case insensitive search NSCompoundPredicate (and/or/not) NSArray *array = [NSArray arrayWithObjects:predicate1, predicate2, nil]; NSPredicate *andPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:array]; Monday, May 10, 2010 Core Data Executing an NSFetchRequest NSFetchRequest *fetchRequest = ...; // create with entity, predicate, etc....
View Full Document

This note was uploaded on 02/15/2011 for the course CS 193P taught by Professor Evandoll during the Spring '09 term at Stanford.

Page1 / 24

Lecture 12_0 - CS193p Spring 2010 Monday, May 10, 2010...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online