I am trying to debug a very nefarious problem with some iPhone Core Data code.
The setup is this: I have a thread which exists to poll a web service and send its results via NSNotification
to the main thread (passed in the userDict
, a bunch of strings and NSNumber
s). I'm using Tim Hatcher's notification library to pass to the main thread.
NSDictionary* userDict = [Message userDictFromXML:el];
if (userDict != nil)
{
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"InsertMessage" object:nil userInfo:userDict];
}
The main thread receives the userDict, then proceeds to extract its values and insert them into a new managed object. So far so good.
I run into problems with this however, but not immediately. After the thread has posted its results to the main thread, and it save a new object into the context, I can perform a certain series of operations (including a NSFetchRequest
and a couple of relationship assignments) which results in a EXC_BAD_ACCESS
when trying to access one of the properties of the fetched managed object.
The other funny thing is that I can make the problem go away. I can do this by putting a single [userDict retain]
in the thread before I put it inside a NSNotification
to be posted into the main thread to be saved into the managed object. It doesn't matter if I firewall the objects from each other by creating a new NSString
as soon as I receive the values in the receiving thread, it will still crash without that retain
.
NSDictionary* userDict = [Message userDictFromXML:el];
if (userDict != nil)
{
[userDict retain]; // NOW THIS WORKS (???)
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"InsertMessage" object:nil userInfo:userDict];
}
What gives ???