October 16, 2012 | 1min read
Discovering bug in iOS Core Data
We recently encountered a very strange bug in iOS Core Data framework. It went as follows the app displayed a preview of a one-to-many relationship using Table Views, after tapping on a table cell a new view displayed the full version. But sometimes toggling between the detail view and back to the preview caused the Table View to display no content.
It occurred under very specific circumstances. The application was for team organisation and the objective was to merge two teams together. The team entity has a one-to-many relationship with the member entity. When we merged two teams we created a team entity object and moved all object relationships to this newly created team entity object. Following this all old objects are deleted. This was all done in a single transaction. We found that after saving the newly created team object the relationship between the team and its members was correct. However, the inverse of this relationship was nil i.e. the members in the newly merged team had no team. In the app this translated to empty teams and a very nasty bug.
It turns out that the reason for this strange behaviour is that during the save Core Data only created the relationship on one side, leaving other blank. It took quite alot of investigation and head scratching to figure out the bug lay in the Core Data framework
The solution was to perform a 2-step transaction. During the first step we create a new team entity object and move the member objects to this new team. On second step we delete old team entity objects.This solves this particularly tricky issue.
Lead Software Engineer