Failed CoreData relationship when retrieving main object

I have a number-to-many entity. I am presenting certain properties of an object in a table using NSFetchedResultsController. Of all the relationships that the entity has, only one of the relationships is displayed (they are currently failing in the cellforrowat method ...). It seems to me that this might have an impact on performance. Is it possible to detect a specific relationship during the creation of a Fetch query so that CoreData doesn't have to fetch values ​​when scrolling through the table?

+2


source to share


3 answers


I am not sure I understand the data model you are describing. If you are only showing members of one of your entity-to-many relationships as the contents of the table rows, you can only display the properties displayed on each of the visible rows using -setPropertiesToFetch: as per your fetch request, for example in the following example:

NSArray *propertiesToFetch = [[NSArray alloc] initWithObjects:@"title", @"thumbnailImage", nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[propertiesToFetch release];

      



However, if what you are describing is a list of entities, with one of the displayed items in the table row being a one-to-one relationship, you can use -setRelationshipKeyPathsForPrefetching:

as Barry suggests. However, in this case, I would suggest denormalizing your data model and moving that property out of relation to being directly related to the original entity. Traversal relationships are much more expensive than property access.

+2


source


First, I would not suggest that the default Core Core behavior is less efficient than the approach you suggest: without data to back up your efforts, the optimization will almost certainly be wrong.



However, I believe it will -[NSFetchRequest setRelationshipKeyPathsForPrefetching:]

do what you want.

+2


source


You can manually create errors in objects, but I don't think you win anything. Whether you are to blame for all of the objects at once or for whatever reason you take turns, each object will be damaged differently as needed.

I've written apps that do exactly what you describe, failing a lot of data to be displayed as a table, and have never noticed any performance degradation. Remember that only those objects will be displayed that correspond to the displayed table cells.

In general, I would say, don't try to outsmart Core Data. At this point, he has years of performance optimization. While intuitively it might seem that a 100 object error would require 100 database queries, this is not necessarily the case.

+1


source







All Articles