Class: NSFetchRequest

Inherits:
NSPersistentStoreRequest show all

Overview

An instance of NSFetchRequest describes search criteria used to retrieve data from a persistent store.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSPersistentStoreRequest

#requestType

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (NSFetchRequest) fetchRequestWithEntityName(entityName)

Returns a fetch request configured with a given entity name. This method provides a convenient way to configure the entity for a fetch request without having to retrieve an NSEntityDescription object. When the fetch is executed, the request uses the managed object context to find the entity with the given name. The model associated with the context’s persistent store coordinator must contain an entity named entityName.

Parameters:

  • entityName (String)

    The name of the entity to fetch.

Returns:

  • (NSFetchRequest)

    A fetch request configured to fetch using the entity named entityName.

Instance Method Details

- (Array) affectedStores

Returns an array containing the persistent stores specified for the receiver.

Returns:

  • (Array)

    An array containing the persistent stores specified for the receiver.

- (NSEntityDescription) entity

Returns the entity specified for the receiver.

Returns:

- (String) entityName

Returns the name of the entity the request is configured to fetch.

Returns:

  • (String)

    The name of the entity the request is configured to fetch.

- (Integer) fetchBatchSize

Returns the batch size of the receiver. The default value is 0. A batch size of 0 is treated as infinite, which disables the batch faulting behavior.If you set a non-zero batch size, the collection of objects returned when the fetch is executed is broken into batches. When the fetch is executed, the entire request is evaluated and the identities of all matching objects recorded, but no more than batchSize objects’ data will be fetched from the persistent store at a time. The array returned from executing the request will be a proxy object that transparently faults batches on demand. (In database terms, this is an in-memory cursor.)You can use this feature to restrict the working set of data in your application. In combination with fetchLimit, you can create a subrange of an arbitrary result set.

Returns:

  • (Integer)

    The batch size of the receiver.

- (Integer) fetchLimit

Returns the fetch limit of the receiver. The fetch limit specifies the maximum number of objects that a request should return when executed.

Returns:

  • (Integer)

    The fetch limit of the receiver.

- (Integer) fetchOffset

Returns the fetch offset of the receiver. The default value is 0.This setting allows you to specify an offset at which rows will begin being returned. Effectively, the request will skip over the specified number of matching entries. For example, given a fetch which would normally return a, b, c, d, specifying an offset of 1 will return b, c, d, and an offset of 4 will return an empty array. Offsets are ignored in nested requests such as subqueries.This can be used to restrict the working set of data. In combination with -fetchLimit, you can create a subrange of an arbitrary result set.

Returns:

  • (Integer)

    The fetch offset of the receiver.

- (NSPredicate) havingPredicate

Returns the predicate used to filter rows being returned by a query containing a GROUP BY. For a full discussion, see setHavingPredicate:. For a discussion of GROUP BY, see setPropertiesToGroupBy:.

Returns:

  • (NSPredicate)

    The predicate used to filter rows being returned by a query containing a GROUP BY.

- (Boolean) includesPendingChanges

Returns a Boolean value that indicates whether, when the fetch is executed it matches against currently unsaved changes in the managed object context. For a full discussion, see includesPendingChanges.

Returns:

  • (Boolean)

    YES if, when the fetch is executed it will match against currently unsaved changes in the managed object context, otherwise NO.

- (Boolean) includesPropertyValues

Returns a Boolean value that indicates whether, when the fetch is executed, property data is obtained from the persistent store. The default value is YES.You can set includesPropertyValues to NO to reduce memory overhead by avoiding creation of objects to represent the property values. You should typically only do so, however, if you are sure that either you will not need the actual property data or you already have the information in the row cache, otherwise you will incur multiple trips to the database.During a normal fetch (includesPropertyValues is YES), Core Data fetches the object ID and property data for the matching records, fills the row cache with the information, and returns managed object as faults (see returnsObjectsAsFaults). These faults are managed objects, but all of their property data still resides in the row cache until the fault is fired. When the fault is fired, Core Data retrieves the data from the row cache—there is no need to go back to the database.If includesPropertyValues is NO, then Core Data fetches only the object ID information for the matching records—it does not populate the row cache. Core Data still returns managed objects since it only needs managed object IDs to create faults. However, if you subsequently fire the fault, Core Data looks in the (empty) row cache, doesn’t find any data, and then goes back to the store a second time for the data.

Returns:

  • (Boolean)

    YES if, when the fetch is executed, property data is obtained from the persistent store, otherwise NO.

- (Boolean) includesSubentities

Returns a Boolean value that indicates whether the receiver includes subentities in the results. The default is YES.

Returns:

  • (Boolean)

    YES if the request will include all subentities of the entity for the request, otherwise NO.

- (Object) initWithEntityName(entityName)

Initializes a fetch request configured with a given entity name This method provides a convenient way to configure the entity for a fetch request without having to retrieve an NSEntityDescription object. When the fetch is executed, the request uses the managed object context to find the entity with the given name. The model associated with the context’s persistent store coordinator must contain an entity named entityName.

Parameters:

  • entityName (String)

    The name of the entity to fetch.

Returns:

  • (Object)

    A fetch request configured to fetch using the entity named entityName.

- (NSPredicate) predicate

Returns the predicate of the receiver. The predicate is used to constrain the selection of objects the receiver is to fetch. For more about predicates, see Predicate Programming Guide.If the predicate is empty—for example, if it is an AND predicate whose array of elements contains no predicates—the receiver has its predicate set to nil. For more about predicates, see Predicate Programming Guide.

Returns:

- (Array) propertiesToFetch

Returns an array of NSPropertyDescription objects that specify which properties should be returned by the fetch. For a full discussion, see setPropertiesToFetch:.

Returns:

- (Array) propertiesToGroupBy

Returns an array of objects that indicate how data should be grouped before a select statement is run in an SQL database. For a full description see setPropertiesToGroupBy:.

Returns:

- (Array) relationshipKeyPathsForPrefetching

Returns the array of relationship key paths to prefetch along with the entity for the request. The default value is an empty array (no prefetching).Prefetching allows Core Data to obtain related objects in a single fetch (per entity), rather than incurring subsequent access to the store for each individual record as their faults are tripped. For example, given an Employee entity with a relationship to a Department entity, if you fetch all the employees then for each print out their name and the name of the department to which they belong, it may be that a fault has to be fired for each individual Department object (for more details, see “Core Data Performance” in Core Data Programming Guide). This can represent a significant overhead. You could avoid this by prefetching the department relationship in the Employee fetch, as illustrated in the following example:

Returns:

  • (Array)

    The array of relationship key paths to prefetch along with the entity for the request.

- (NSFetchRequestResultType) resultType

Returns the result type of the receiver. The default value is NSManagedObjectResultType.You use setResultType: to set the instance type of objects returned from executing the request—for possible values, see “NSFetchRequestResultType.” If you set the value to NSManagedObjectIDResultType, this will demote any sort orderings to “best efforts” hints if you do not include the property values in the request.

Returns:

  • (NSFetchRequestResultType)

    The result type of the receiver.

- (Boolean) returnsDistinctResults

Returns a Boolean value that indicates whether the fetch request returns only distinct values for the fields specified by propertiesToFetch. The default value is NO.

Returns:

  • (Boolean)

    YES if, when the fetch is executed, it returns only distinct values for the fields specified by propertiesToFetch, otherwise NO.

- (Boolean) returnsObjectsAsFaults

Returns a Boolean value that indicates whether the objects resulting from a fetch using the receiver are faults. The default value is YES. This setting is not used if the result type (see resultType) is NSManagedObjectIDResultType, as object IDs do not have property values. You can set returnsObjectsAsFaults to NO to gain a performance benefit if you know you will need to access the property values from the returned objects. By default, when you execute a fetch returnsObjectsAsFaults is YES; Core Data fetches the object data for the matching records, fills the row cache with the information, and returns managed object as faults. These faults are managed objects, but all of their property data resides in the row cache until the fault is fired. When the fault is fired, Core Data retrieves the data from the row cache. Although the overhead for this operation is small, for large datasets it may become non-trivial. If you need to access the property values from the returned objects (for example, if you iterate over all the objects to calculate the average value of a particular attribute), then it is more efficient to set returnsObjectsAsFaults to NO to avoid the additional overhead.

Returns:

  • (Boolean)

    YES if the objects resulting from a fetch using the receiver are faults, otherwise NO.

- (Object) setAffectedStores(stores)

Sets the array of persistent stores that will be searched by the receiver.

Parameters:

  • stores (Array)

    An array containing identifiers for the stores to be searched when the receiver is executed.

Returns:

- (Object) setEntity(entity)

Sets the entity of the receiver.

Parameters:

Returns:

- (Object) setFetchBatchSize(bsize)

Sets the batch size for the receiver. For a full discussion, see fetchBatchSize.

Parameters:

  • bsize (Integer)

    The batch size of the receiver.A batch size of 0 is treated as infinite, which disables the batch faulting behavior.

Returns:

- (Object) setFetchLimit(limit)

Sets the fetch limit of the receiver. For a full discussion, see fetchLimit.

Parameters:

  • limit (Integer)

    The fetch limit of the receiver. 0 specifies no fetch limit.

Returns:

- (Object) setFetchOffset(limit)

Sets the fetch offset of the receiver. For a full discussion, see fetchOffset.

Parameters:

  • limit (Integer)

    The fetch offset of the receiver.

Returns:

- (Object) setHavingPredicate(predicate)

Sets the predicate to use to filter rows being returned by a query containing a GROUP BY. If a having predicate is supplied, it will be run after the GROUP BY. Specifying a HAVING predicate requires that a GROUP BY also be specified. For a discussion of GROUP BY, see setPropertiesToGroupBy:.

Parameters:

  • predicate (NSPredicate)

    The predicate to use to filter rows being returned by a query containing a GROUP BY.

Returns:

- (Object) setIncludesPendingChanges(yesNo)

Sets if, when the fetch is executed, it matches against currently unsaved changes in the managed object context. The default value is YES.If the value is NO, the fetch request skips checking unsaved changes and only returns objects that matched the predicate in the persistent store.

Parameters:

  • yesNo (Boolean)

    If YES, when the fetch is executed it will match against currently unsaved changes in the managed object context.

Returns:

- (Object) setIncludesPropertyValues(yesNo)

Sets if, when the fetch is executed, property data is obtained from the persistent store. For a full discussion, see includesPropertyValues.

Parameters:

  • yesNo (Boolean)

    If NO, the request will not obtain property information, but only information to identify each object (used to create managed object IDs).

Returns:

- (Object) setIncludesSubentities(yesNo)

Sets whether the receiver includes subentities.

Parameters:

  • yesNo (Boolean)

    If NO, the receiver will fetch objects of exactly the entity type of the request; if YES, the receiver will include all subentities of the entity for the request (if any).

Returns:

- (Object) setPredicate(predicate)

Sets the predicate of the receiver.

Parameters:

  • predicate (NSPredicate)

    The predicate for the receiver.

Returns:

- (Object) setPropertiesToFetch(values)

Specifies which properties should be returned by the fetch. The property descriptions may represent attributes, to-one relationships, or expressions. The name of an attribute or relationship description must match the name of a description on the fetch request’s entity.

Parameters:

Returns:

- (Object) setPropertiesToGroupBy(array)

Sets array of objects that indicate how data should be grouped before a select statement is run in an SQL database. If you use this setting, then you must set the resultType to NSDictionaryResultType, and the SELECT values must be literals, aggregates, or columns specified in the GROUP BY. Aggregates will operate on the groups specified in the GROUP BY rather than the whole table. If you set properties to group by, you can also set a having predicate—see setHavingPredicate:.

Parameters:

Returns:

- (Object) setRelationshipKeyPathsForPrefetching(keys)

Sets an array of relationship key paths to prefetch along with the entity for the request. For a full discussion, see relationshipKeyPathsForPrefetching.

Parameters:

  • keys (Array)

    An array of relationship key-path strings in NSKeyValueCoding notation (as you would normally use with valueForKeyPath:).

Returns:

- (Object) setResultType(type)

Sets the result type of the receiver. For further discussion, see resultType.

Parameters:

  • type (NSFetchRequestResultType)

    The result type of the receiver.

Returns:

- (Object) setReturnsDistinctResults(values)

Sets whether the request should return only distinct values for the fields specified by propertiesToFetch. For a full discussion, see returnsDistinctResults.

Parameters:

  • values (Boolean)

    If YES, the request returns only distinct values for the fields specified by propertiesToFetch.

Returns:

- (Object) setReturnsObjectsAsFaults(yesNo)

Sets whether the objects resulting from a fetch request are faults. For a full discussion, see returnsObjectsAsFaults.

Parameters:

  • yesNo (Boolean)

    If NO, the objects returned from the fetch are pre-populated with their property values (making them fully-faulted objects, which will immediately return NO if sent the isFault message). If YES, the objects returned from the fetch are not pre-populated.

Returns:

- (Object) setShouldRefreshRefetchedObjects(flag)

Sets whether the fetch request should cause property values of fetched objects to be updated with the current values in the persistent store. By default, when you fetch objects they maintain their current property values, even if the values in the persistent store have changed. By invoking this method with the parameter YES, when the fetch is executed the property values of fetched objects to be updated with the current values in the persistent store. This provides more convenient way to ensure managed object property values are consistent with the store than by using refreshObject:mergeChanges: (NSManagedObjetContext) for multiple objects in turn.

Parameters:

  • flag (Boolean)

    YES if the fetch request should cause property values of fetched objects to be updated with the current values in the persistent store, otherwise NO.

Returns:

- (Object) setSortDescriptors(sortDescriptors)

Sets the array of sort descriptors of the receiver.

Parameters:

  • sortDescriptors (Array)

    The array of sort descriptors of the receiver. nil specifies no sort descriptors.

Returns:

- (Boolean) shouldRefreshRefetchedObjects

Returns a Boolean value that indicates whether the property values of fetched objects will be updated with the current values in the persistent store. For a full discussion, see setShouldRefreshRefetchedObjects:.

Returns:

  • (Boolean)

    YES if the property values of fetched objects will be updated with the current values in the persistent store, otherwise NO.

- (Array) sortDescriptors

Returns the sort descriptors of the receiver. The sort descriptors specify how the objects returned when the fetch request is issued should be ordered—for example by last name then by first name. The sort descriptors are applied in the order in which they appear in the sortDescriptors array (serially in lowest-array-index-first order).

Returns:

  • (Array)

    The sort descriptors of the receiver.