Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Wednesday, December 18, 2013

iOS Core Data NSFetchRequest Fetch All Entities Example

So, you want a example of performing an NSFetchRequest in CoreData to fetch all of one kind of Entity, huh?

Lets say you have an entity called Events, which is a schedule of upcoming things you want to list in a table view... now, the whole UITableView thing, we won't go into that now (we'll link to that discussion below), but lets just suppose that it suffices that we want to fetch the list of events, heck, all of the event entity objects we have on hand. How would we do that?

Here's an example I cooked up, which fetches all event entities from the database and spits out an error on the console if anything goes wrong:

+(NSArray *)getAllEventsInManagedObjectContext:(NSManagedObjectContext *)context
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Event"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1==1"];
    [request setPredicate:predicate];
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:descriptor];
    NSError *error = nil;
    NSArray *events = [context executeFetchRequest:request error:&error];
      NSLog(@"%@", error);
    return events;

"That's all well and good, you say, but I need an example that shows how to use predicates".. if this is the case, checkout our post on "iOS NSFetchRequest with Predicates Example".

If you'd like to hear more about displaying entities in a UITableView, checkout our article on "How to Show Entities in a UITableView in iOS".

If you found this article helpful, why not give us a +1. And if you do, thank you very much!

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates