Wednesday, 13 May 2015

Flush entity in Core Data













If you need to delete or flush all the data that exist in core data entity then use this method. Just pass the name of the Entity as a param to this method.
  1. -(void)flushEntity:(NSString*)entityName{
  2. NSFetchRequest *fetchAllObjects = [[NSFetchRequest alloc] init];
  3. [fetchAllObjects setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self.myContext]];
  4. [fetchAllObjects setIncludesPropertyValues:NO]; //only fetch the managedObjectID
  5. NSError *erors = nil;
  6. NSArray *allObjects = [self.myContext executeFetchRequest:fetchAllObjects error:&erors];
  7. // uncomment next line if you're NOT using ARC
  8. // [allObjects release];
  9. if (erors) {
  10. NSLog(@"Error occurred1");
  11. }else
  12. NSLog(@"Error occurred1");
  13. for (NSManagedObject *object in allObjects) {
  14. [self.myContext deleteObject:object];
  15. }
  16. }

For such more Blogs you can visit to http://findnerd.com/NerdDigest

No comments:

Post a Comment