2

I have a to-one relationship between a Document and Settings model:

alt text

On creation of a Document, a corresponding Settings object is created. The models are persisted to core data.

When relaunching after creating a few documents (and the associate settings), the application reloads a list of documents to select using:

// Load delegate from shared application and context from delegate.
SampleAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;

// Create new request.
NSFetchRequest *request = [[NSFetchRequest alloc] init];

// Create entity description using delegate object context.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:context];

// Set entity for request.
[request setEntity:entity];

// Load array of documents.
NSError *error;
NSArray *documents = [context executeFetchRequest:request error:&error];

// Rlease descriptor.
[descriptor release];

// Release request.
[request release];

// Done.
return documents;

The list appears fine (the documents are loading). However, when attempting to access the settings for any of the documents the program exits giving:

Program received signal: “SIGABRT”.

The modifications to the settings are triggered when a user selects a button. Strangely enough, the program does not crash if the following snippet is added just before returning the documents on load (i.e. if the relationships are accessed while loading):

for (Document *document in documents)
{
    NSLog(@"document.name: %@", document.name);
    NSLog(@"document.settings.stroke: %@", document.settings.stroke);
    NSLog(@"document.settings.stroke: %@", document.settings.stroke);
}

The property for the relationship is "(nonatomic, retain)" and uses "@dynamic". Any ideas? Thanks!

Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
  • Did you run with Zombies? The code looks fine, I suspect some sort of memory management problem. – Martin Brugger Aug 27 '10 at 22:03
  • @martin, I'm not that familiar with the XCode performance tools, but when I select "Run > Run With Performance Tool" Zombies is greyed out. Anything special I have to do to use it? Thanks. – Kevin Sylvestre Aug 27 '10 at 22:12
  • NSZombie enabled description here: http://stackoverflow.com/questions/1211923/how-to-use-nszombie-in-xcode – NWCoder Aug 27 '10 at 22:59
  • I suggest you post the code where it actually crashes. It could be as simple as a typo. – TechZen Aug 28 '10 at 21:15

2 Answers2

0

In your Core Data model, did you set up an inverse relationship between Document & Settings? If not, you must.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83
0

After spending way too much time on this bug, I finally figured it out. During the initial load of the values, I setting a KVO. This code was causing crashes the crashes (although they were not appearing until the next run loop):

[self.document.settings addObserver:self forKeyPath:@"fill" options:0 context:NULL];
[self.document.settings addObserver:self forKeyPath:@"stroke" options:0 context:NULL];

Not sure why it was an issue, but I was lucky enough to be able to simply remove it for now.

Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232