我想省略 NSFetchRequest 中的谓词,以便删除实体的所有托管对象。
但是,当没有谓词时(根据 SQL 调试器),不会执行获取请求。根据苹果公司的说法,谓词应该是可选的。
我需要如何更改代码才能删除谓词?有任何想法吗?谢谢!
- (void)deleteEntity:(NSString*)entityName inContext:(NSManagedObjectContext *)context
{
NSFetchRequest * request= [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"TableStructure" inManagedObjectContext:context]];
//[entities setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"category = 'est'"];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *entitiesArray = [context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"%@: Error fetching context: %@", [self class], [error localizedDescription]);
NSLog(@"entitiesArray: %@",entitiesArray);
return;
}
for(NSManagedObject *entity in entitiesArray) {
[context deleteObject:entity];
}
NSError *saveError = nil;
[context save:&saveError];
}
更新2
看来我在数据库准备好之前调用了我的获取请求。如何确保在核心数据数据库准备好之前我的请求不会被调用?
最佳答案
如果您不需要谓词,则无需分配它。删除以下行:
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"category = 'est'"];
[request setPredicate:predicate];
关于objective-c - 核心数据获取请求中需要谓词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10294851/