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, October 3, 2012

Deleting an NSManagedObject

How to Delete an NSManageObject in iOS Core Data...

// Lets assume we already have a manage object context ... from our created database

// we'll call managedDatabaseObject
NSManagedObjectContext *context = managedDatabaseObject.managedObjectContext;

// So, well create a request to fetch our Person we wish to delete
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];

// And we'll filter our request by the person id we have on hand (assumedly) 
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %@", personId];
[request setPredicate:predicate];

// And we have to specify a sortDescriptor every time we fetch, regardless
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"last_name" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];

NSError *error;
NSArray *results = [moc executeFetchRequest:request error:&error];

if (results == nil)
    // Deal with error...
} else {
  NSManagedObject *person = [results lastObject];
  [context deleteObject:person];

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