我正在使用我的应用中的核心数据。在那我使用下面的功能删除和插入核心数据数据库表中的数据。它第一次工作正常。当我第二次打电话时,它会抛出EXC BAD ACCESS。 任何人都可以帮忙解决这个问题。
@implementation abc
@synthesize delegate;
Dynamic *dbObjDynamic;
-(void) callingcoredata:(NSInteger) family type:(NSInteger) type List:(NSString *) list
{
if ([AppManager getAppMode] == Online)
{
statdyn = type;
arrDyanmic= [[NSMutableArray alloc]init];
dbObjDynamic = [[Dynamic alloc]init];
[dbObjDynamic DeleteRecordControlsFromTable:listtype];
}
}
在Dynamic.m中
-(void)DeleteRecordControlsFromTable:(NSString *)listtype
{
NSManagedObjectContext *deleteContext = [self ManagedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Dynamic" inManagedObjectContext:deleteContext]];
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"listType == %@", listtype];
[fetch setPredicate:predicate];
NSError * error = nil;
NSArray * lookupdata = [deleteContext executeFetchRequest:fetch error:&error];
for(NSManagedObject *info in lookupdata)
{
[deleteContext deleteObject:info];
[deleteContext save:&error];
}
}
它会在dbObjDynamic = [[Dynamic alloc]init];
提前致谢。
答案 0 :(得分:0)
你应该将/ init分配给Dynamic
managedObject类的get实例,而不是使用它 -
Dynamic *dbObjDynamic = [self.fetchedResultsController objectAtIndexPath:specificIndex];
并调用这些方法从managedObjectContext获取Dynamic
类managedObject的fetchedResults
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dynamic" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"anyAttributeOfDynamic"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Core data error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}