views:

53

answers:

2

I am getting a weird crash when I try to save my model. This is my code:

 TJModel *model = [TJModel sharedTJModel];
 NSFetchRequest *request = [[[NSFetchRequest alloc] init]autorelease];
 [request setReturnsObjectsAsFaults:NO];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"TJVideoList"inManagedObjectContext:[model managedObjectContext]];
 [request setEntity:entity];

 NSError *error = nil;
 NSMutableArray *mutableFetchResults = [[[model managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];

if (error != nil)
  NSLog(@"error %@",[error localizedDescription]);


 TJVideoList *videoList = nil;

 if ([mutableFetchResults count] == 0) {

  videoList = (VideoList *)[NSEntityDescription insertNewObjectForEntityForName:@"TJVideoList" 
                 inManagedObjectContext:[model managedObjectContext]];
 }
 else 
 {
  videoList = [mutableFetchResults objectAtIndex:0];
 }


 [videoList addVideoListObject:recordedVideo];
 error = nil;

 if (![[model managedObjectContext] save:&error]) {

And crash .....This is what said in the terminal:

-[NSConcreteValue UTF8String]: unrecognized selector sent to instance 0x1d33f0

I thought it might be a cuestion of deallocated objects, so I retained them like this:

[managedObjectContext setRetainsRegisteredObjects:YES];

With no luck.

+1  A: 

Sounds more like you assigned an NSValue instance (NSNumber, most likely, as it is the most commonly used subclass) where an NSString was expected. The -retainsRegisteredObjects: is unlikely to be needed (it won't fix a memory related problem anyway).

It might possibly be an over-release issue, too. Try running with Zombie detection enabled (see the Run menu).

bbum
+1  A: 

Your crash is not a result of this code.

Crashes in saves usually result from an error with an managedObject's attributes. In this case, you have somewhere assigned the wrong value to a string attribute. When the context goes to convert the string attribute to a UTF8 string for persistence, the object that is there instead of the NSString does not understand the message and the crash results.

Although this code should run okay, you do have some risky practices:

NSFetchRequest *request = [[[NSFetchRequest alloc] init]autorelease];

This is a bad practice. autorelease is the same as release. You should not send it to an object until you are complete done with it. autorelease marks an object for death the next time the memory pool is drained. In some case, that will kill the object unexpectedly. While it won't cause problems here, you don't want to get into the habit of taking this short cut because it will eventually bite you.

You should only use autorelease when the current scope is done with the object but the object is being sent outside the scope (usually in a method return.)

NSMutableArray *mutableFetchResults = [[[model managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];

The mutable array here is pointless as is the copy. This is apparently in some reference material somewhere because it keeps cropping up novices code in the last few months. If you're not going to alter an array there is no reason to have it mutable. In the case of an array of managed objects, it is pointless to copy the array.

videoList = [mutableFetchResults objectAtIndex:0]

Since you have no sort descriptor for the fetch, the mutableFetchResults array will be in a random order. If you have more than one object returned, which is almost always the case, you will get a random TJVideoList object at the zero element every time you run the code.

TechZen
@TechZen btw that `-mutableCopy` junk is in an Apple example. I have filed a radar against it. It is also in the Head Start iPhone Development book (copied from said Apple documentation).
Marcus S. Zarra
@TechZen Thanks a lot for all for your info and specially the suggestions, they are invaluable. And as Marcus remarked, I took the code from an Apple example. All that glitters is not gold!
toupper
@Marcus S. Zarra -- Ah ha! Now that we found the vampires lair we can drive a stake in its heart!
TechZen
@TechZen I have been ruminating about this, with no outcome. I expect the Array MutableFetchRequest to be one long, as I do not have more TJVideoList objects than one in my datamodel, that is why I take the zero position of the array. Isn't it correct? Which would it be the best approach? Thanks a lot.
toupper
If you know for certain that you have only one TJVideoList object then it would be safe to to always access the zero element of the array returned by the fetch. However, that would a very, very unusual design. Are you sure you are not confusing the abstract TJVideoList entity in the data model, of which there is only one, with the live instantiated objects, of which there maybe an arbitrary number? The fetch returns the latter.
TechZen