1

I have a problem with core data when setting relationships:

The code below crashes randomly when setting the relationships between country and region.

If I disable the second for-loop, the method completes without errors. Everything happens within the context living on the background-thread.

Again: I can create the objects for the regions and countries without trouble and they show up in the Simulators database just fine - but as soon as I try to set the relationships between then, the app crashes randomly.

Any thoughts ?

for (Region* region in regions) {        
    // only store if region code isn't empty
    if (region.m_RegionCode != nil && [region.m_RegionCode length] > 0) { 
        NSManagedObject* cdRegion = [NSEntityDescription insertNewObjectForEntityForName:CDREGION inManagedObjectContext:self.objectContextBackground];
        [cdRegion setValue:region.m_RegionCode forKey:@"code"];
        [cdRegion setValue:region.m_regioncodedescription forKey:@"name"];            
    }            
}
[self saveBackgroundContext];
 for (Region* region in regions) {        
    if (region.m_RegionCode != nil && [region.m_RegionCode length] > 0) { 
        NSManagedObject* cdRegion = [self getManagedObject:CDREGION withCode:region.m_RegionCode];
        NSManagedObject* CDCountry = [self getManagedObject:CDCOUNTRY withCode:region.m_countrycode];
        [cdRegion setValue:CDCountry forKey:@"country"];

    }            
}

2 Answers 2

1

well - just to let you know: it actually was the problem (I know - it is listed on top of all pages regarding this topic :-) that I used a context between threads.

I mixed up queues and threads. I created a single background-queue where I used my "background-context" ... but of course i created several threads within that, who where interacting with the context... so...

btw: it was this excellent article that finally clarified it for me: (came right in time :-) http://www.cimgf.com/2011/08/22/importing-and-displaying-large-data-sets-in-core-data/

0

This error message is likely related to memory problem, about using (or releasing) already deallocated object. You should run profiler to spot the memory problem, or debug with a breakpoint after the save method and following a line by line execution until it crashes.

Just guessing, if you are creating or getting the Regions object with some sort of factory method, probably it gets deallocated in the middle of execution by that method, sometimes the routine is fast enough to complete before the dealloc, sometimes not, that could explain the randomness.

Try to retain the Regions at the beginning and release at the end of the second loop.

1
  • hi,thanks for responding, I should have mentioned that all objects seem to be valid when the app crashes. The region object as well as the coreData Objects. Any further guess ?
    – HeikoG
    Aug 22, 2011 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.