Core Data crash when removing entities from an ordered relationship

Originator:yellowfruit
Number:rdar://12418689 Date Originated:02-Oct-2012 09:25 PM
Status:Open Resolved:
Product:Other Product Version:iOS 5.1/6.0
Classification: Reproducible:Always
 
02-Oct-2012 09:25 PM Matthew Sibson:
Summary:

Calling the generated method to remove entities from an ordered relationship crashes at runtime.

Steps to Reproduce:

- (void)triggerBug
{
	// Get all the books
	NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:self.managedObjectContext];
	[fetchRequest setEntity:entity];
	NSError *error = nil;
	NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
	
	// Make a shelf
	Shelf *shelf = [NSEntityDescription insertNewObjectForEntityForName:@"Shelf" inManagedObjectContext:self.managedObjectContext];
	
	// Add all books
	for (Book *book in fetchedObjects) {
		[shelf addBooksObject:book];
	}
	
	// Remove a subset
	NSOrderedSet *booksToRemove = [NSOrderedSet orderedSetWithArray:fetchedObjects range:NSMakeRange(0, 3) copyItems:NO];
	[shelf removeBooks:booksToRemove];
}

Expected Results:

Three of the books are removed from the relationship.

Actual Results:

Application crashes, with this log output:

2012-10-02 21:04:53.476 CoreDataBooks[37705:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSSet intersectsSet:]: set argument is not an NSSet'
*** First throw call stack:
(0x15c0012 0x13e5e7e 0x164ea4a 0x60e796 0x5910c9 0x60e603 0xf6768 0x87f8069 0x290c 0x25a3 0x93180f 0x931e01 0x932fab 0x944315 0x94524b 0x936cf8 0x240ddf9 0x240dad0 0x1535bf5 0x1535962 0x1566bb6 0x1565f44 0x1565e1b 0x9327da 0x93465c 0x241d 0x2345)
libc++abi.dylib: terminate called throwing an exception

Regression:

iOS 5.1 and iOS 6.0 have this issue, you'll note I've used another approach to add books instead of the addBooks: method, as that fails with the same exception.

This issue can be worked around by looping around addBooksObject: or removeBooksObject: as appropriate.

Notes:

The attached zip contains the above code snippet within the app delegate, the project is a modified copy of your own Core Data Books sample code. I added a model version in order to add the appropriate relationships in order to demonstrate this bug.

The project is for iPhone, we're seeing this bug in the wild on iPad, it seems to be platform agnostic.

02-Oct-2012 09:25 PM Matthew Sibson:
'CoreDataBooks.zip' was successfully uploaded

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!