Skip to content
Browse files

Added a method to delete all data for given entityname

  • Loading branch information...
1 parent 727a164 commit 47ecab4a55555421dba3bd89c71a6dcd776dd332 @jk committed
Showing with 43 additions and 0 deletions.
  1. +15 −0 NSManagedObjectContextAdditions.h
  2. +28 −0 NSManagedObjectContextAdditions.m
View
15 NSManagedObjectContextAdditions.h
@@ -0,0 +1,15 @@
+//
+// NSManagedObjectContextAdditions.h
+// CD Test
+//
+// Created by Jens Kohl on 11.11.09.
+// Copyright 2009 milch & zucker AG. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSManagedObjectContext (JKAdditions)
+-(void)deleteAllObjectsForEntity:(NSString *)entityName;
+
+@end
View
28 NSManagedObjectContextAdditions.m
@@ -0,0 +1,28 @@
+//
+// NSManagedObjectContextAdditions.m
+// CD Test
+//
+// Created by Jens Kohl on 11.11.09.
+// Copyright 2009 milch & zucker AG. All rights reserved.
+//
+
+#import "NSManagedObjectContextAdditions.h"
+
+
+@implementation NSManagedObjectContext (JKAdditions)
+
+-(void)deleteAllObjectsForEntity:(NSString *)entityName {
+ NSManagedObjectContext *context = self;
+ NSFetchRequest *allEntities = [[NSFetchRequest alloc] init];
+ [allEntities setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:context]];
+
+ NSError *error = nil;
+ NSArray *allObjects = [context executeFetchRequest:allEntities error:&error];
+ [allEntities release];
+
+ for (NSManagedObject *object in allObjects) {
+ [context deleteObject:object];
+ }
+}
+
+@end

0 comments on commit 47ecab4

Please sign in to comment.
Something went wrong with that request. Please try again.