Monday, 11 April 2011

Core data basics

So core data. Turns out I actually like this bit of IPhone coding. It makes ORM / DB stuff really easy.

Samble code to load and sort a 'fugitive' object from an SQLLite DB

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
bountyHunterAppDelegate *appDelegate = (bountyHunterAppDelegate*)[[UIApplicaion sharedApplication] delgate];
NSManagedObjectContext *managedObectContext = appDelegate.managedObjectContext;
// Create request object to get data
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:managedObectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error;
NSMutableArray *mutablesFetchResults = [[managedObectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutablesFetchResults == Nil) {
NSLog(@"all buggered");
self.items = mutableCopy;
[mutablesFetchResults release];
[request release];

No comments:

Post a Comment