开发者

"[CFString release]: message sent to deallocated instance" when using CoreData

开发者 https://www.devze.com 2023-02-13 11:38 出处:网络
I have started using CoreData in my project. Without the pieces of code from CodeData my project was working pretty fine. I added the methods for accessing the NSManagedObjectContext from the coreData

I have started using CoreData in my project. Without the pieces of code from CodeData my project was working pretty fine. I added the methods for accessing the NSManagedObjectContext from the coreData project template. Now I try to create new CoreData object with the following code:

- (void)saveSearchResultToHistory:(NSArray *) productsArray  {
   [productsArray retain];

   NSLog(@"context: %@", self.managedObjectContext);

   Product *product = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"Product" 
                                   inManagedObjectContext:self.managedObjectContext];
product.productId = [(Product *) [productsArray objectAtIndex:0] productId];

NSError *error;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}


[productsArray release];
}

When this method is ran once then everything is fine, when I try to run it for the second time, the processing is stopped at:

Product *product = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"Product" 
                                   inManagedObjectContext:self.managedObjectContext];

with the following error message in the console:

[CFString retain]: message sent to deallocated instance 0x5a23b0

Any ideas what might be开发者_C百科 wrong? Thanks!


First off you do not need to save the context every time you add something, just save when the app closes or goes in the background.

The error you are getting looks like you over release a NSString some where.

To check if the error isn't in the coredata context use this save function:

- (void)saveContext {
    if ([self.managedObjectContext hasChanges]) {
        NSError *error = nil;
        if (![self.managedObjectContext save:&error]) {

            dbgPrint(@"Failed to save to data store: %@", [error localizedDescription]);

            NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];

            if (detailedErrors != nil && [detailedErrors count] > 0) {
                for(NSError* detailedError in detailedErrors) {
                    dbgPrint(@"--DetailedError: %@", [detailedError userInfo]);
                }
            } else {
                dbgPrint(@"  %@", [error userInfo]);
            }
        }
    }   
}    
0

精彩评论

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