views:

78

answers:

2

I have some trouble with a core data project. I have a NSArrayController with NSManagedObjects in it, and + / - buttons. If i delete a object in the row after the file has been saved it works perfectly, but if I add a new object, and immediately delete it again (without changing any of the default values), i get an error:

Serious application error.  Exception was caught during Core Data change processing: Unknown number type or nil passed to arithmetic function expression. with userInfo (null)  
Unknown number type or nil passed to arithmetic function expression.  

It's the same case if i undo the add of the new object.

Anybody able to give me a pointer what to do? I assume it has something to do with that it only has an temporary ID, but how to solve it i don't know.

A: 

Tables don't usually contain NSManagedObjects. Rather, either an NSArrayController is providing the managed objects' attributes as values via bindings or an object conforming NSTableViewDataSource is. The deletion of a selected object provided in either of these scenarios can be done a myriad of ways. The problem is, you haven't explained at all how your case is set up.

Is it possible your deletion (whatever target/action your button is wired to) is being passed nil or something unexpected? I can imagine a similarly-frighenting error if you asked a managed object context to delete a float value or some other random thing ...

Joshua Nozzi
I'm sorry i was a little to quick in the description. It is as you say a NSArrayController that is holding the objects, and is binded to a NSTableView. I've tried to put in a check wheather the object i'm about to delete is correct, and it is. There is nothing wrong with it. The error also first occures some moments later (1 or more runloops)
Jonas Jongejan
Ssssooooo ..... how *exactly* are you deleting the object? How *exactly* are things bound? Help us help you.
Joshua Nozzi
The bindings are completely after the book. NSArrayController set to Entity mode and my object as Entity name. The table is binded pr. column on a property. I don't think the problem is here. - I've tried different delete methods. They all produce the same error (both [arrayController removeObject:theObject] and [managedObjectContext deleteObject:theObject]) but as i say, the error is also when i press undo. So just by sayin add object -> undo the error is produced. Any idea what the error actually means?
Jonas Jongejan
I have been looking around, and got one suspect: Multiple contexts. Im not asking anywhere to make multiple context, but i'm worried i got. I don't know if rings any bells.
Jonas Jongejan
A: 

I found the problem and solution. For the record i will describe it.

The problem was that the object in its init function added an observer on itself. This was probably what triggered the error. If i before i delete the object remove itself as observer, the error is not produced.

Jonas Jongejan
You should **NOT** override the `-init...` method of an `NSManagedObject` nor its `-dealloc` method for that matter. If you need to add an observer you should do it in the `-awakeFromInsert` and `-awakeFromFetch`.
Marcus S. Zarra
According to documentation "Methods to Override Considerations" both initWithEntity:insertIntoManagedObjectContext: amd dealloc are allowed to be overwritten? The reason i use init instead of awake, is because i find that awake is not always callen (if a user undo or something). But i might be wrong.
Jonas Jongejan
you are right about undo and redo, in 10.6+ there is the additional method awakeFromSnapshotEvent which is also called in undo/redo. As a workaround for 10.5 I changed ManagedObjectContext to allow undo and redo with working observings http://github.com/mbrugger/CoreDataDependentProperties/blob/master/LPAutomatedObserving/Classes/LPManagedObjectContext.m
Martin Brugger
But is there anything wrong doing it in initWithEntity? As long as I call super?
Jonas Jongejan