NSObjectController Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in OS X v10.3 and later. |
| Declared in | NSObjectController.h |
| Companion guides | |
Overview
NSObjectController is a Cocoa bindings-compatible controller class. Properties of the content object of instances of this class can be bound to user interface elements to access and modify their values.
By default, the content of an NSObjectController instance is an NSMutableDictionary object. This allows a single NSObjectController instance to be used to manage many different properties referenced by key-value paths. The default content object class can be changed by calling setObjectClass:, which subclasses must override. Your application should use a custom data class that is key-value compliant whenever possible.
Object Controllers, Entity Mode, and Lazy Fetching
NSObjectController and its subclasses, when in entity mode, can now fetch lazily. With lazy fetching enabled using the method setUsesLazyFetching:, the controller will try to fetch only a small amount of data from available persistent stores. This can provide a significant improvement in memory use when a large amount of content is stored on disk but just a subset of that data is required in memory.
When set to use lazy fetching, a controller will fetch objects in batches. You can change the default batch size for your application by setting a value for the the user default "com.apple.CocoaBindings.LazyFetchBatchSize". If you have table views bound to an array controller set to use lazy fetching, the size of the controller's batch size will grow as the table views' visible row count grows.
Add, Insert, and Remove operations on controllers that use lazy fetching behave similarly to the same operations on a regular controller. The difference is that it is faster to sort an array controller using lazy fetching if:
All of the keys in the
sortDescriptorsarray are modeled, non transient properties.All of the selectors in the
sortDescriptorsarray arecompare:orcaseInsensitiveCompare:.There are no changes in the controller's managed object context
Tasks
Initializing an Object Controller
Managing Content
-
– setContent: -
– content -
– setAutomaticallyPreparesContent: -
– automaticallyPreparesContent -
– prepareContent
Setting the Content Class
Managing Objects
Managing Editing
Core Data Support
-
– entityName -
– setEntityName: -
– fetch: -
– setUsesLazyFetching: -
– usesLazyFetching -
– defaultFetchRequest -
– fetchPredicate -
– setFetchPredicate: -
– managedObjectContext -
– setManagedObjectContext: -
– fetchWithRequest:merge:error:
Obtaining Selections
Validating User Interface Items
Instance Methods
add:
Creates a new object and sets it as the receiver’s content object.
Parameters
- sender
Typically the object that invoked this method.
Discussion
Creates a new object of the appropriate entity (specified by entityName) or class (specified by objectClass)—see newObject—and sets it as the receiver’s content object using addObject:.
Special Considerations
Beginning with OS X v10.4 the result of this method is deferred until the next iteration of the runloop so that the error presentation mechanism can provide feedback as a sheet.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.haddObject:
Sets the receiver’s content object.
Parameters
- object
The content object for the receiver.
Discussion
If the receiver's content is bound to another object or controller through a relationship key, the relationship of the “master” object is changed.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hautomaticallyPreparesContent
Returns a Boolean value that indicates whether the receiver automatically prepares its content when it is loaded from a nib.
Return Value
YES if the receiver automatically prepares its content when loaded from a nib, otherwise NO.
Discussion
See setAutomaticallyPreparesContent: for a full explanation of "automatically prepares content."
The default is NO.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hcanAdd
Returns a Boolean value that indicates whether an object can be added to the receiver using add:.
Return Value
YES if an object can be added to the receiver using add:, otherwise NO.
Discussion
Bindings can use this method to control the enabling of user interface objects.
This property is observable using key-value observing.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hcanRemove
Returns a Boolean value that indicates whether an object can be removed from the receiver.
Return Value
YES if an object can be removed from the receiver using remove:, otherwise NO.
Discussion
Bindings can use this method to control the enabling of user interface objects.
This property is observable using key-value observing.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hcontent
Returns the receiver’s content object.
Return Value
The receiver’s content object.
Discussion
This property is observable using key-value observing.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hdefaultFetchRequest
Returns the default fetch request used by the receiver.
Return Value
The default NSFetchResult used by the receiver.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSObjectController.hentityName
Returns the entity name used by the receiver to create new objects.
Return Value
The entity name used by the receiver to create new objects.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSObjectController.hfetch:
Causes the receiver to fetch the data objects specified by the entity name and fetch predicate.
Parameters
- sender
Typically the object that invoked this method.
Special Considerations
Beginning with OS X v10.4 the result of this method is deferred until the next iteration of the runloop so that the error presentation mechanism can provide feedback as a sheet.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSObjectController.hfetchPredicate
Returns the receiver’s fetch predicate.
Return Value
The receiver’s fetch predicate.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSObjectController.hfetchWithRequest:merge:error:
Subclasses should override this method to customize a fetch request, for example to specify fetch limits.
Parameters
- fetchRequest
The fetch request to use for the fetch. Pass
nilto use the default fetch request.- merge
If
YES, the receiver merges the existing content with the fetch result, otherwise the receiver replaces the entire content with the fetch result.- error
If an error occurs, upon return contains an
NSErrorobject that describes the problem.
Return Value
YES if the fetch completed successfully, otherwise NO.
Discussion
This method performs a number of actions that you cannot reproduce. To customize this method, you should therefore create your own fetch request and then invoke super’s implementation with the new fetch request.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSObjectController.hinitWithContent:
Initializes and returns an NSObjectController object with the given content.
Parameters
- content
The content for the receiver.
Return Value
The initialized object controller, with its content object set to content.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hisEditable
Returns a Boolean value that indicates whether the receiver allows adding and removing objects.
Return Value
YES if the receiver allows adding and removing objects, otherwise NO.
Discussion
This property is observable using key-value observing.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hmanagedObjectContext
Returns the receiver’s managed object context.
Return Value
The receiver’s managed object context.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSObjectController.hnewObject
Creates and returns a new object of the appropriate class.
Return Value
A new object of the appropriate class. The returned object is implicitly retained, the sender is responsible for releasing it (with either release or autorelease).
If an entity name is set (see setEntityName:), the object created is an instance of the class specified for that entity (and the object is inserted into the receiver's managed object context). Otherwise the object created is an instance of the class returned by objectClass.
Discussion
This method is called when adding and inserting objects if automaticallyPreparesContent is YES.
The default implementation assumes the class returned by objectClass has a standard init method without arguments. If the object class being controlled is NSManagedObject (or a subclass thereof) its designated initializer (initWithEntity:insertIntoManagedObjectContext:) is called instead, using the entity and managed object context specified for the receiver.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hobjectClass
Returns the class used when creating new non-Core Data objects.
Return Value
The object class used when creating new non-Core Data objects (that is, if no entity has been set)—see newObject.
Discussion
If an entity has been set, then the class returned by this method does not automatically reflect the class for the entity.
The default class is NSMutableDictionary.
This property is observable using key-value observing.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hprepareContent
Typically overridden by subclasses that require additional control over the creation of new objects.
Discussion
Subclasses that implement this method are responsible for creating the new content object and setting it as the receiver’s content object. This method is only called if automaticallyPreparesContent has been set to YES.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hremove:
Removes the receiver’s content object.
Parameters
- sender
Typically the object that invoked this method.
Discussion
Removes the receiver’s content object using removeObject:.
Special Considerations
Beginning with OS X v10.4 the result of this method is deferred until the next iteration of the runloop so that the error presentation mechanism can provide feedback as a sheet.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hremoveObject:
Removes a given object from the receiver’s content.
Parameters
- object
The object to remove from the receiver.
Discussion
If object is the receiver’s content object, the receiver’s content is set to nil. If the receiver's content is bound to another object or controller through a relationship key, the relationship of the ‘master’ object is cleared.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hselectedObjects
Returns an array of all objects to be affected by editing.
Return Value
An array of all objects to be affected by editing. If the receiver supports a selection mechanism, the array contains key value coding compliant proxies of the selected objects; otherwise proxies for all content objects are returned. If the receiver is a concrete instance of NSObjectController, returns an array containing the receiver’s content object.
Discussion
You should avoid registering for key-value observing changes for key paths that pass through this method, (for example, selectedObjects.firstName). Using the proxy returned by the selection method is better for performance.
This property is observable using key-value observing.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hselection
Returns a proxy object representing the receiver’s selection.
Return Value
A proxy object representing the receiver’s selection. This object is fully key-value coding compliant, but note that it is a proxy and so does not provide the full range of functionality that might be available in the source object.
Discussion
If a value requested from the selection proxy using key-value coding returns multiple objects, the controller has no selection, or the proxy is not key-value coding compliant for the requested key, the appropriate marker (NSMultipleValuesMarker, NSNoSelectionMarker or NSNotApplicableMarker) is returned. Otherwise, the value of the key is returned.
This property is observable using key-value observing.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hsetAutomaticallyPreparesContent:
Sets whether the receiver automatically creates and inserts new content objects automatically when loading from a nib file.
Parameters
- flag
A flag that specifies whether the receiver automatically prepares its content.
Discussion
If flag is YES and the receiver is not using a managed object context, prepareContent is used to create the content object. If flag is YES and a managed object context is set, the initial content is fetched from the managed object context using the current fetch predicate. The default is NO.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hsetContent:
Sets the receiver’s content object.
Parameters
- content
The content object for the receiver.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hsetEditable:
Sets whether the receiver allows adding and removing objects.
Parameters
- flag
YESif the the receiver should allow adding and removing objects, otherwiseNO.
Discussion
The default is YES.
Availability
- Available in OS X v10.3 and later.
See Also
Declared In
NSObjectController.hsetEntityName:
Sets the entity name used by the receiver to create new objects.
Parameters
- entityName
The entity name used by the receiver to create new objects.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSObjectController.hsetFetchPredicate:
Sets the receiver’s fetch predicate.
Parameters
- predicate
The fetch predicate for the receiver.
Discussion
The receiver uses predicate when fetching its content, for example in fetch:. If you need to customize the fetching behavior further, you can override fetchWithRequest:merge:error:.
Availability
- Available in OS X v10.4 and later.
Declared In
NSObjectController.hsetManagedObjectContext:
Sets the receiver’s managed object context.
Parameters
- managedObjectContext
The managed object context for the receiver.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSObjectController.hsetObjectClass:
Sets the object class to use when creating new objects.
Parameters
- objectClass
The object class to use when creating new objects.
Discussion
NSObjectController's default implementation assumes that instances of objectClass are initialized using a standard init method that takes no arguments.
If an entity name has been set (see setEntityName:), this method has no effect.
Availability
- Available in OS X v10.3 and later.
Declared In
NSObjectController.hsetUsesLazyFetching:
Sets whether the receiver uses lazy fetching.
Parameters
- enabled
Boolean value that indicates whether the receiver uses lazy fetching.
Discussion
When enabled the controller uses a number of techniques that typically make managing large data sets more efficient. As with all optimizations, you should use suitable performance analysis tools (such as Instruments) to determine the best solution.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSObjectController.husesLazyFetching
Returns a Boolean indicating whether the receiver uses lazy fetching.
Return Value
YES if the receiver uses lazy fetching, otherwise NO.
Availability
- Available in OS X v10.5 and later.
Declared In
NSObjectController.hvalidateUserInterfaceItem:
Returns whether the receiver can handle the action method for a user interface item.
Return Value
YES if the receiver can handle the action method; NO if it cannot.
Availability
- Available in OS X v10.5 and later.
Declared In
NSObjectController.h© 2010 Apple Inc. All Rights Reserved. (Last updated: 2010-11-29)