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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5519212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:42:52  来源:igfitidea点击:

How to completely clear out a Core Data model?

iphoneobjective-cxcodecore-dataios4

提问by max_

How I could completely clear out EVERYTHING in a Core Data model i.e remove all objects for all entities?

我如何才能完全清除核心数据模型中的所有内容,即删除所有实体的所有对象?

I will be using the code to clear out the saved history in a the model.

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

回答by Mak083

Here is another way to do...

这是另一种方法...

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];

It will clear all the objects.

它将清除所有对象。

回答by Jamie Chapman

Delete the .sqlite DB Core Data creates (it's probably stored on your /Documents directory) and get your app to re-create it.

删除 .sqlite DB Core Data 创建的(它可能存储在您的 /Documents 目录中)并让您的应用程序重新创建它。

回答by Mike A

Here is a good answer to this question.

这是这个问题的一个很好的答案。

Delete/Reset all entries in Core Data?

删除/重置 Core Data 中的所有条目?

Then just make sure to re-create the store.

然后只需确保重新创建商店。