Strange error inserting new relationship data in core-data
- by michael
My app will allow users to create a personalised list of events from a large list of events. I have a table view which simply displays these events, tapping on one of them takes the user to the event details view, which has a button "add to my events".
In this detailed view I own the original event object, retrieved via an NSFetchedResultsController and passed to the detailed view (via a table cell, the same as the core data recipes sample). I have no trouble retrieving/displaying information from this "event". I am then trying to add it to the list of MyEvents represented by a one to many (inverse) relationship:
This code:
NSManagedObjectContext *context = [event managedObjectContext];
MyEvents *myEvents = (MyEvents *)[NSEntityDescription insertNewObjectForEntityForName:@"MyEvents" inManagedObjectContext:context];
[myEvents addEventObject:event];//ERROR
And this code (suggested below):
//would this add to or overwrite the "list" i am attempting to maintain
NSManagedObjectContext *context = [event managedObjectContext];
MyEvents *myEvents = (MyEvents *)[NSEntityDescription insertNewObjectForEntityForName:@"MyEvents" inManagedObjectContext:context];
NSMutableSet *myEvent = [myEvents mutableSetValueForKey:@"event"];
[myEvent addObject:event]; //ERROR
Bot produce (at the line indicated by //ERROR):
*** -[NSComparisonPredicate evaluateWithObject:]: message sent to deallocated instance
Seems I may have missed something fundamental. I cant glean any more information through the use of debugging tools, with my knowledge of them.
1) Is this a valid way to compile and store an editable list like this?
2) Is there a better way?
3) What could possibly be the deallocated instance in error?
--
I have now modified the Event entity to have a to-many relationship called "myEvents" which referrers to itself. I can add Events to this fine, and logging the object shows the correct memory addresses appearing for the relationship after a [event addMyEventObject:event];. The same failure happens right after this however. I am still at a loss to understand what is going wrong. This is the backtrace
#0 0x01f753a7 in ___forwarding___ ()
#1 0x01f516c2 in __forwarding_prep_0___ ()
#2 0x01c5aa8f in -[NSFetchedResultsController(PrivateMethods) _preprocessUpdatedObjects:insertsInfo:deletesInfo:updatesInfo:sectionsWithDeletes:newSectionNames:treatAsRefreshes:] ()
#3 0x01c5d63b in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] ()
#4 0x0002e63a in _nsnote_callback ()
#5 0x01f40005 in _CFXNotificationPostNotification ()
#6 0x0002bef0 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#7 0x01bbe17d in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] ()
#8 0x01c1d763 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] ()
#9 0x01ba25ea in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] ()
#10 0x01bdfb3a in -[NSManagedObjectContext processPendingChanges] ()
#11 0x01bd0957 in _performRunLoopAction ()
#12 0x01f4d252 in __CFRunLoopDoObservers ()
#13 0x01f4c65f in CFRunLoopRunSpecific ()
#14 0x01f4bc48 in CFRunLoopRunInMode ()
#15 0x0273878d in GSEventRunModal ()
#16 0x02738852 in GSEventRun ()
#17 0x002ba003 in UIApplicationMain ()
Cheers.