Created
March 2, 2012 12:52
-
-
Save patelrohan/1958227 to your computer and use it in GitHub Desktop.
Fetch Request Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)fetchRecords { | |
// Define our table/entity to use | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]; | |
// Setup the fetch request | |
NSFetchRequest *request = [[NSFetchRequest alloc] init]; | |
[request setEntity:entity]; | |
// Define how we will sort the records | |
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; | |
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; | |
[request setSortDescriptors:sortDescriptors]; | |
[sortDescriptor release]; | |
// Fetch the records and handle an error | |
NSError *error; | |
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; | |
if (!mutableFetchResults) { | |
// Handle the error. | |
// This is a serious error and should advise the user to restart the application | |
} | |
// Save our fetched data to an array | |
[self setEventArray: mutableFetchResults]; | |
[mutableFetchResults release]; | |
[request release]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment