NSFetchedResultsController duplicates records between sections

Originator:steve.madsen
Number:rdar://11256532 Date Originated:2012-04-16
Status:new Resolved:
Product:Other Product Version:iOS 5.1
Classification:Serious Bug Reproducible:Always
 
Summary:
NSFetchedResultsController does not properly section records when initialized with a non-nil sectionNameKeyPath, the initial set of results contains no objects and the object count increases beyond 1 during the lifetime of the NSFetchedResultsController instance.

Steps to Reproduce:
1. Run the attached project in the iOS simulator. This project is the template master/detail project from Xcode 4.3.2 for iPhone. I changed the data model to include one additional attribute on the Event model, section. This is an integer that flip-flops between 0 and 1 every time a record is added. The NSFetchedResultsController is initialized with a sectionNameKeyPath using this new attribute. I also added a -report method that shows detail about how NSFetchedResultsController partitions results into sections and what objects are in those sections.

2. Observe the console output as you click the + button to add one record, then a second record.

Expected Results:
The -report method gives an view into the results, without a UITableView to hide the bug.

When the + button is tapped the second time, I expect there to be two sections (0 and 1), each with a single object. numberOfObjects for each section should return 1, and the objects array should show a single object matching up with the row visible in the UITableView.

Actual Results:
After the second record is added, section "0" reports a single object, but examining the actual contents of the objects array reveals there are actually TWO objects. The second object of this array is ALSO the sole object contained in the objects array of section "1".

This is the console output from running it here:

2012-04-16 14:03:43.939 FetchedResultsBug[25299:fb03] total results = 1
2012-04-16 14:03:43.941 FetchedResultsBug[25299:fb03] total sections = 1
2012-04-16 14:03:43.942 FetchedResultsBug[25299:fb03] section 0, “0”, 1 objects
2012-04-16 14:03:43.944 FetchedResultsBug[25299:fb03] (
    "<NSManagedObject: 0x85254c0> (entity: Event; id: 0x8525510 <x-coredata:///Event/t80760E7D-3192-4151-9330-272C3242934A2> ; data: {\n    section = 0;\n    timeStamp = \"2012-04-16 18:03:43 +0000\";\n})"
)
2012-04-16 14:03:54.475 FetchedResultsBug[25299:fb03] total results = 2
2012-04-16 14:03:54.476 FetchedResultsBug[25299:fb03] total sections = 2
2012-04-16 14:03:54.477 FetchedResultsBug[25299:fb03] section 0, “0”, 1 objects
2012-04-16 14:03:54.477 FetchedResultsBug[25299:fb03] (
    "<NSManagedObject: 0x85254c0> (entity: Event; id: 0x852a6b0 <x-coredata://01392D91-D822-49A9-A850-C59E12D6E2A5/Event/p21> ; data: {\n    section = 0;\n    timeStamp = \"2012-04-16 18:03:43 +0000\";\n})",
    "<NSManagedObject: 0x6b244a0> (entity: Event; id: 0x6b07070 <x-coredata:///Event/t80760E7D-3192-4151-9330-272C3242934A3> ; data: {\n    section = 1;\n    timeStamp = \"2012-04-16 18:03:54 +0000\";\n})"
)
2012-04-16 14:03:54.478 FetchedResultsBug[25299:fb03] section 1, “1”, 1 objects
2012-04-16 14:03:54.479 FetchedResultsBug[25299:fb03] (
    "<NSManagedObject: 0x6b244a0> (entity: Event; id: 0x6b07070 <x-coredata:///Event/t80760E7D-3192-4151-9330-272C3242934A3> ; data: {\n    section = 1;\n    timeStamp = \"2012-04-16 18:03:54 +0000\";\n})"
)

Regression:

Notes:
This bug is not a problem if the data is displayed using a UITableView, because tableView:numberOfRowsInSection: tells the table view to only query for that many records. UI that works with data from NSFetchedResultsController by asking for the objects within a section directly will show duplicated records in the UI.

This bug does not occur UNLESS there are zero records in the results when the NSFetchedResultsController is created.

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!