Database Reference
In-Depth Information
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@ "Person"
inManagedObjectContext:[self managedObjectContext]]];
This option is a very good middle ground between fetching faults and some
of the following choices. In situations where only the object requested needs
to be displayed right away and its relationships are not needed right away,
this can be the most efficient solution.
How to Load Property Values and NSManagedObjectID Objects
We can also combine this option with the NSManagedObjectID retrieval listed
earlier to warm up the cache. The settings to accomplish this are as follows:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setResultType:NSManagedObjectIDResultType];
[fetchRequest setEntity:[NSEntityDescription entityForName:@ "Person"
inManagedObjectContext:[self managedObjectContext]]];
This can be used to excellent effect on a background thread when the entire
fetch is going to take a significant amount of time. Once the NSManagedObjectID
objects are retrieved, they can be safely passed to the primary thread and
used to display the data to the user. Using Core Data within a multithreaded
application is discussed in more detail in Chapter 5, Threading , on page 77 .
Loading Relationships
The next step up in the scale of loading data is to prefetch the relationships
while loading the targeted entities. This does not fetch them as fully formed
but as faults. This step up can have a significant impact on the performance
of a Core Data application. In the test, this fetch took 1.166 seconds to retrieve
3,417 objects, each with only a single object on the other side of a one-to-one
relationship. With a more complex data model, this becomes an even larger
performance hit.
How to Load Relationships
Fortunately, this option gives us some fine-grained control over which rela-
tionships to load. This would allow us to, for example, load only the
addresses associated with a person and skip over their images, phone
numbers, and so on. Accomplishing this requires passing an NSArray of NSString
objects with the names of the relationships to load.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSArray *relationshipKeys = [NSArray arrayWithObject:@ "addresses" ];
[fetchRequest setRelationshipKeyPathsForPrefetching:relationshipKeys];
[fetchRequest setEntity:[NSEntityDescription entityForName:@ "Person"
inManagedObjectContext:[self managedObjectContext]]];
 
 
Search WWH ::




Custom Search