Get data from a table

//Link Filter+ Sort :
Send Snippet To: Save this snippet to Code Collector Pro -- view all haxis's snippets
language: Objective-C
license: Other

Code for Snippet:

//Solution 1 CoreDataTableViewController
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
- (void)viewWillAppear:(BOOL)animated
    [super viewWillAppear:animated];
    [self setupFetchedResultsController];
- (void)setupFetchedResultsController
    // 1 - Decide what Entity you want
    NSString *entityName = @"Role"; // Put your entity name here
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
    // 2 - Request that Entity
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
    // 3 - Filter it if you want
    //request.predicate = [NSPredicate predicateWithFormat:@" = Blah"];
    // 4 - Sort it if you want
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
    // 5 - Fetch it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
    [self performFetch];
//Role *role = [self.fetchedResultsController objectAtIndexPath:indexPath];
//Solution 2
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
    NSLog(@"Name: %@", [info valueForKey:@"name"]);
    NSManagedObject *details = [info valueForKey:@"details"];
    NSLog(@"Zip: %@", [details valueForKey:@"zip"]);
comments powered by Disqus


Tags: Coredata

Link to this snippet:

Download to Code Collector

To use the direct link to your snippet on either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 734
Date Added: 2012-11-21 11:34:56
Last Modified: 0000-00-00 00:00:00

Web Analytics