调用NSManagedObject方法会抛出EXC BAD ACCESS

时间:2015-06-18 14:42:53

标签: ios objective-c core-data exc-bad-access nsmanagedobject

我正在使用我的应用中的核心数据。在那我使用下面的功能删除和插入核心数据数据库表中的数据。它第一次工作正常。当我第二次打电话时,它会抛出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];

处抛出错误

提前致谢。

1 个答案:

答案 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;
}