如何彻底清除核心数据模型?

max*_*ax_ 1 iphone xcode core-data objective-c ios4

如何在核心数据模型中完全清除一切,即删除所有实体的所有对象?

我将使用代码清除模型中保存的历史记录.

Mak*_*083 10

这是另一种方法......

NSManagedObjectContext *context = [appDelegate managedObjectContext];
            NSFetchRequest * allMovies = [[NSFetchRequest alloc] init];
            [allMovies setEntity:[NSEntityDescription entityForName:@"Movies" inManagedObjectContext:context]];
            [allMovies setIncludesPropertyValues:NO]; //only fetch the managedObjectID

            NSError * error = nil;
            NSArray * movies = [context executeFetchRequest:allMovies error:&error];
            //error handling goes here
            for (NSManagedObject * movie in movies) {
                [context deleteObject:movie];
            }
            NSError *saveError = nil;
            [context save:&saveError];
Run Code Online (Sandbox Code Playgroud)

它将清除所有对象.

  • 正确答案. (2认同)