检查核心数据的结果并删除

时间:2014-03-25 11:36:54

标签: ios objective-c core-data

我需要为我的核心数据创建一个方法。此方法应检查实体SectionFixture中是否有任何结果。如果有则删除所有og中的对象。

我该怎么做?我试过这个,但什么也没做。

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *numberOfFixtures = [NSEntityDescription
                insertNewObjectForEntityForName:@"Fixture"
                inManagedObjectContext:context];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fixture" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
self.theFixtures = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy];

for (numberOfFixtures *fixture in self.theFixtures) {
    [context deleteObject:fixture];
}

NSError *error;
[context save:&error];

1 个答案:

答案 0 :(得分:1)

试试这个,

 NSManagedObjectContext *context = [self managedObjectContext];
 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fixture" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
 self.theFixtures = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy];

 for (NSManagedObject *fixture in self.theFixtures) {
     [context deleteObject:fixture];
 }
 NSError *error;
 [context save:&error];