Calling a NSManagedObject method throws EXC BAD ACCESS

159 views Asked by At

I am working with core data in my app. In that i am using below function to delete and insert data in core data database table. It works fine at first time. When i call 2nd time it throws EXC BAD ACCESS. Can anyone help to solve this.

@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];
}
}

In 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];
}

}

It throws error at dbObjDynamic = [[Dynamic alloc]init];

Thanks in advance.

1

There are 1 answers

0
AudioBubble On

You should alloc/init to the get instance of Dynamic managedObject class, Instead use this -

Dynamic *dbObjDynamic = [self.fetchedResultsController objectAtIndexPath:specificIndex];

and also call these method to get the fetchedResults of the Dynamic class managedObject from the managedObjectContext

- (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;
}