Reputation: 5634
I would like to omitt the predicate in an NSFetchRequest in order to delete all managed objects for an entity.
However, when there is no predicate (according to the SQL debugger), the fetch request is not executed. According to Apple the predicate should be optional.
How would I need to change my code to remove the predicate? Any ideas? Thank you!
- (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];
}
It seems that I call my fetch request before the database is ready. How can I make sure that my request is not called before the core data database is ready?
Upvotes: 2
Views: 3388
Reputation: 5634
I issued my fetch request before the Core Data database was ready.
To solve this issue, I have now added the call ImportFormulasInRequest
to the UIDocument openWithCompletion handler, which encapsulates my core data database:
- (void)useDocument
{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.myDatabase.fileURL path]]) {
// does not exist on disk, so create it
[self.myDatabase saveToURL:self.myDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
[self setupFetchedResultsController];
}];
} else if (self.myDatabase.documentState == UIDocumentStateClosed) {
// exists on disk, but we need to open it
[self.myDatabase openWithCompletionHandler:^(BOOL success) {
[self setupFetchedResultsController];
[self ImportFormulasInContext:[self.myDatabase managedObjectContext]];
}];
} else if (self.myDatabase.documentState == UIDocumentStateNormal) {
// already open and ready to use
[self setupFetchedResultsController];
[self ImportFormulasInContext:[self.myDatabase managedObjectContext]];
}
}
Upvotes: 0
Reputation: 1152
You simply don't assign the predicate if you don't want it. Remove following lines:
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"category = 'est'"];
[request setPredicate:predicate];
Upvotes: 1