开发者

Memory leak when retrieving data from database

开发者 https://www.devze.com 2023-02-18 13:54 出处:网络
Hey, I\'ve created a custom retrieval method for database access: +(NSArray*) recordsForTable:(NSString *)table predicate:(NSPredicate *)prd{

Hey, I've created a custom retrieval method for database access:

+(NSArray*) recordsForTable:(NSString *)table predicate:(NSPredicate *)prd{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:table inManagedObjectContext:managedObjectContext];

    [fetchRequest setEntity:entity];

    [fetchRequest setPredicate:prd]开发者_JS百科;

    NSArray *records = [managedObjectContext executeFetchRequest:fetchRequest error:nil];

    [fetchRequest release];

    return records;
}

i then use the above method in this method:

-(NSArray *)tableViewControllerData{

    NSNumber *savedBool = [[NSNumber alloc] initWithBool:YES];

    NSString *onlyGetSavedVisitObjects = [NSString stringWithFormat:@"bolSaved=%@", savedBool];
    [savedBool release];
    NSMutableArray *data = [[[CoreDataAccess recordsForTable:@"LPVisit" stringPredicate:onlyGetSavedVisitObjects] mutableCopy] autorelease];
    NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dteVisitDate" ascending:NO];
    NSArray *descriptors = [NSArray arrayWithObjects:dateDescriptor, nil];
    [data sortedArrayUsingDescriptors:descriptors];

    return data;    
}

The trouble am having is that when the user makes changes to the LPVisit table and recall this method to show those changes it crashes the application.

[EDIT]

The exception it produces is:

 -[__NSArrayM objectID]: unrecognized selector sent to instance 0x4dac1f0

I believe the error is at line:

NSMutableArray *data = [[[CoreDataAccess recordsForTable:@"LPVisit" stringPredicate:onlyGetSavedVisitObjects] mutableCopy] autorelease];

If I remove the autorelease, I get a memory leak but the application doesn't crash.

Does anyone have any insights, thanks in advance


Is it possible that the mutable copying is throwing the exception because *records is nil? This might happen if @"bolSaved=%@" is a typo and should be @"boolSaved=%@".

0

精彩评论

暂无评论...
验证码 换一张
取 消