Load Data From Core Data Example Source Code

On November 24, 2014, in iPhone iPad Object-C, by James Liu

Core data is the simplest way to save and load local data in ios apps. In previous tutorial, I already gave some example source code to show you how efficient it is to save data in core data. I am pretty sure you will get more exciting once you see how easy to fetch data form core data and filter core data results.

Load Data From Core Data

Before we try to load data from core data, we need NSManagedObjectContext instance (How to get NSManagedObjectContext instance). Once we get NSManagedObjectContext instance ready, we can follow the below steps to load data from core data.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserProfile" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

//build sort rule
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"username" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

//set fetch size
[fetchRequest setFetchBatchSize:5];

NSFetchedResultsController *fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"fetch_result_cache"];
NSError * error;
[fetchedResultController performFetch:&error];

NSFetchedResultsController will hold the all result loaded from core data. If we want to get the first UserProfile, we can use following way to get the result:

NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;
UserProfile *user = [fetchedResultController objectAtIndexPath:myIP];
NSLog(@"%@", user.username);

Above example load the whole UserProfile data from core data without any filter. In next example, I will show you a piece of code which will filter the core data result. In the source code, I will use the same NSFetchedResultsController instance we instantiated before. I will apply a fetch predicate on the NSFetchRequest, so the result will be filtered by the predicates.

//the resultsController is the object we created before
NSFetchRequest *fetchRequest = resultsController.fetchRequest;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username = %@", @"James"];
[fetchRequest setPredicate:predicate];
NSError * error;
[resultsController performFetch:&error];

Now, we fetch a new set of data with filter. The predicate will only get UserProfile whose username equals to “James”. Please keep in mind, the result is a set of data. We can use the same way to get the first record of the core data fetch result.

NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;
UserProfile *user = [fetchedResultController objectAtIndexPath:myIP];
NSLog(@"%@", user.username);

Get Full App Source Code Under $6.99

If you are still having problems with Core Data, I suggest you to get my original source code. The source code covers all topics in my tutorial example. And I also create an singleton class which you can easily use in your own project by copy and paste. There is no restriction on using the source code in your own project.


Leave a Reply

Premium WordPress Themes