WWW.BOOK.DISLIB.INFO
FREE ELECTRONIC LIBRARY - Books, dissertations, abstract
 
<< HOME
CONTACTS



Pages:     | 1 |   ...   | 6 | 7 || 9 | 10 |   ...   | 27 |

«Introduction to Core Data Programming Guide 11 Who Should Read This Document 11 Organization of This Document 11 See Also 13 Technology Overview 14 ...»

-- [ Page 8 ] --

[self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];

[[self primitiveEmployees] addObject:value];

[self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation

–  –  –

}

- (void)removeEmployeesObject:(Employee *)value { NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];

[self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];

[[self primitiveEmployees] removeObject:value];

[self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation

–  –  –

}

- (void)addEmployees:(NSSet *)value { [self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation

–  –  –

2012-09-19 | Copyright © 2004, 2012 Apple Inc. All Rights Reserved.

Managed Object Accessor Methods Custom Primitive Accessor Methods [[self primitiveEmployees] unionSet:value];

[self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation

–  –  –

[[self primitiveEmployees] minusSet:value];

[self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation

–  –  –

} Custom Primitive Accessor Methods Primitive accessor methods are similar to "normal" or public key-value coding compliant accessor methods, except that Core Data uses them as the most basic data methods to access data, consequently they do not issue key-value access or observing notifications. Put another way, they are to primitiveValueForKey: and

setPrimitiveValue:forKey: what public accessor methods are to valueForKey: and setValue:forKey:.

Typically there should be little reason to implement primitive accessor methods. They are, however, useful if you want custom methods to provide direct access to instance variables for persistent Core Data properties.

The example below contrasts public and primitive accessor methods for an attribute, int16, of type Integer 16, stored in a custom instance variable, nonCompliantKVCivar.

// primitive get accessor

- (short)primitiveInt16 { return nonCompliantKVCivar;

} // primitive set accessor 2012-09-19 | Copyright © 2004, 2012 Apple Inc. All Rights Reserved.

Managed Object Accessor Methods Custom Primitive Accessor Methods

- (void)setPrimitiveInt16:(short)newInt16 { nonCompliantKVCivar = newInt16;

} // public get accessor

- (short)int16 {

–  –  –

// public set accessor

- (void)setInt16:(short)int16 { [self willChangeValueForKey: @"int16"];

nonCompliantKVCivar = int16;

[self didChangeValueForKey:@"int16"];

The Core Data Framework relieves you from the need to implement many of the mechanisms needed to manage data-bearing (model) objects. It does, though, impose the requirement that model objects are instances of, or instances of classes that inherit from, NSManagedObject, and that the model objects are properly integrated in to the Core Data infrastructure. This document first describes the basic pieces of the infrastructure you need to create a managed object, and how to easily instantiate an instance of a managed object and integrate it into that infrastructure. It then describes the processes that are abstracted by the convenience methods you typically use to create a managed object; how to assign an object to a particular store; and finally how to delete a managed object.

Creating, Initializing, and Saving a Managed Object A managed object is an instance of an Objective-C class. From this perspective, it is no different from any other object you use—you can simply create an instance using alloc. A managed object differs from other objects

in three main ways—a managed object:

Must be an instance of NSManagedObject or of a class that inherits from NSManagedObject ● Exists in an environment defined by its managed object context ● Has an associated entity description that describes the properties of the object ● In principle, there is therefore a lot of work to do to create a new managed object and properly integrate it into the Core Data infrastructure. In practice, however, this task is made easy by a convenience class method (insertNewObjectForEntityForName:inManagedObjectContext:) of NSEntityDescription. The following example shows the easiest way to create a new instance of an entity named “Employee”.

NSManagedObject *newEmployee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:context];

The method returns an instance of whatever class is defined in the managed object model to represent the entity, initialized with the default values given for its entity in the model.

2012-09-19 | Copyright © 2004, 2012 Apple Inc. All Rights Reserved.

Creating and Deleting Managed Objects Behind the Scenes of Creating a Managed Object In many cases the default values you set in the model may be sufficient. Sometimes, however, you may wish to perform additional initialization—perhaps using dynamic values (such as the current date and time) that cannot be represented in the model. In a typical Cocoa application you would override the class’s init method to provide this functionality. With NSManagedObject, you are discouraged from overriding initWithEntity:insertIntoManagedObjectContext:; instead, Core Data provides several other means of initializing values—these are described in “Object Life-Cycle—Initialization and Deallocation” (page 41).





Simply creating a managed object does not cause it to be saved to a persistent store. The managed object context acts as a scratchpad. You can create and register objects with it, make changes to the objects, and undo and redo changes as you wish. If you make changes to managed objects associated with a given context,

those changes remain local to that context until you commit the changes by sending the context a save:

message. At that point—provided that there are no validation errors—the changes are committed to the store.

See also “Assigning an Object to a Store” (page 59).

Behind the Scenes of Creating a Managed Object Although NSEntityDescription’s convenience method makes it easy to create and configure a new managed object, it may be instructive to detail what is happening behind the scenes. If this is not of current interest, you may safely skip this section (go to “Assigning an Object to a Store” (page 59))—you are encouraged, however, to revisit this material to ensure that you fully understand the process.

In order to properly integrate a managed object into the Core Data infrastructure there are two elements you

need:

A managed object context ● An entity description ● The Managed Object Context The context is responsible for mediating between its managed objects and the rest of the Core Data infrastructure. The infrastructure is in turn responsible for, for example, translating changes to managed objects into undo actions maintained by the context, and also into operations that need to be performed on the persistent store with which the managed object is assigned.

The context is in effect also your gateway to the rest of the Core Data infrastructure. As such, it is expected that you either keep a reference to the context, or you have a means of easily retrieving it—for example, if you are developing a document-based application that uses NSPersistentDocument, you can use the document class’s managedObjectContext method.

2012-09-19 | Copyright © 2004, 2012 Apple Inc. All Rights Reserved.

Creating and Deleting Managed Objects Behind the Scenes of Creating a Managed Object The Entity Description An entity description specifies (amongst other things) the name of an entity, the class used to represent the entity, and the entity’s properties. The entity description is important since a given class may be used to represent more than one entity—by default all entities are represented by NSManagedObject. Core Data uses the entity description to determine what properties a managed object has, what needs to be saved to or retrieved from the persistent store, and what constraints there are on property values. Entity descriptions are properties of a managed object model.

Given a managed object context, you could retrieve the appropriate entity description through the persistent

store coordinator as illustrated in the following example:

NSManagedObjectContext *context = #Get a context#;

NSManagedObjectModel *managedObjectModel = [[context persistentStoreCoordinator] managedObjectModel];

NSEntityDescription *employeeEntity = [[managedObjectModel entitiesByName] objectForKey:@"Employee"];

In practice, you would use the convenience method entityForName:inManagedObjectContext: of

NSEntityDescription which does the same thing—as illustrated in the following example:

NSManagedObjectContext *context = /* assume this exists */;

NSEntityDescription *employeeEntity = [NSEntityDescription

–  –  –

Creating a Managed Object Fundamentally NSManagedObject is an Objective-C class like any other Objective-C class. Like various other classes, NSManagedObject imposes some constraints on instance creation. As described earlier, you must associate the new managed object instance with the entity object that defines its properties and with the managed object context that defines its environment. You cannot therefore initialize a managed object simply by sending an init message, you must use the designated initializer—initWithEntity:insertIntoManagedObjectContext:—which sets both the entity and

context:

NSManagedObject *newEmployee = [[NSManagedObject alloc]

–  –  –

This is in effect what NSEntityDescription’s convenience method insertNewObjectForEntityForName:inManagedObjectContext: does for you—including the entity instance look-up described in “The Entity Description” (page 58). This is why you should typically use that

method rather than NSManagedObject’s initWithEntity:insertIntoManagedObjectContext:.

An important additional point here is that initWithEntity:insertIntoManagedObjectContext: returns an instance of the class specified by the entity description to represent the entity. If you want to create a new Employee object and in the model you specified that the Employee entity should be represented by a custom class, say Employee, it returns an instance of Employee. If you specified that the Employee entity should be represented by NSManagedObject, it returns an instance of NSManagedObject.

Assigning an Object to a Store Typically there is only one persistent store for a given entity, and Core Data automatically ensures that new objects are saved to this store when the object's managed object context is saved. Sometimes, however, you may have multiple writable stores for a given entity—for example you may store some data in a specific document and some in a common global repository (say, a store in the user’s Application Support folder). In this situation you must specify the store in which the object is to reside.

You specify the store for an object using the NSManagedObjectContext method, assignObject:toPersistentStore:. This method takes as its second argument the identifier for a store.

You obtain the store identifier from the persistent store coordinator, using for example persistentStoreForURL:. The following example illustrates the complete process of creating a new managed object and assigning it to a global store.

NSURL *storeURL = #URL for path to global store#;

id globalStore = [[context persistentStoreCoordinator] persistentStoreForURL:storeURL];

NSManagedObject *newEmployee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:context];

[context assignObject:newEmployee toPersistentStore:globalStore];

–  –  –

Of course, the object is not saved to the store until the managed object context is saved.

Deleting a Managed Object

Deleting a managed object is straightforward. You simply send its managed object context a deleteObject:

message, passing the object you want to delete as the argument.



Pages:     | 1 |   ...   | 6 | 7 || 9 | 10 |   ...   | 27 |


Similar works:

«© Rafkin Film/Jürgen Olczyk SAFER INTERNET DAY 2016 EXTREM IM NETZ Presseinformation der EU-Initiative klicksafe zum internationalen Aktionstag für mehr Sicherheit im Internet am 9. Februar 2016 Safer Internet Day 2016: „Extrem im Netz“ EU-Initiative klicksafe koordiniert Aktionstag für Online-Sicherheit am 9. Februar Zum diesjährigen Safer Internet Day rückt klicksafe den Schwerpunkt „Extrem im Netz“ in den Mittelpunkt. Die EUInitiative koordiniert die vielfältigen Maßnahmen...»

«Observations on cerebral amyloid angiopathy and microvascular pathology in Alzheimer's disease and vascular dementia Haglund, Mattias Published: 2005-01-01 Link to publication Citation for published version (APA): Haglund, M. (2005). Observations on cerebral amyloid angiopathy and microvascular pathology in Alzheimer's disease and vascular dementia Department of Clinical Sciences, Lund University General rights Copyright and moral rights for the publications made accessible in the public portal...»

«Deepening Democracy The Real Utopias Project Series editor: Erik Olin Wright The Real Utopias Project embraces a tension between dreams and practice. It is founded on the belief that what is pragmatically possible is not fixed independently of our imaginations, but is itself shaped by our visions. The fulfillment of such a belief involves ‘real utopias’: utopian ideals that are grounded in the real potentials for redesigning social institutions. In its attempt at sustaining and deepening...»

«More on art is not a verb Donald Brook A lecture prompted by the Flinders University Art Museum exhibition ‘Art as a verb’ (14 Feb-26 April 2015), delivered at the Hetzel Lecture Theatre in the Institute Building, Adelaide, on 19 March 2015. Explaining exhibitions like Art as a verb1 is difficult because objects are implicitly contrasted with processes in a puzzling way. I offered some remarks about this at the opening on 19 February,2 and I’m grateful to the exhibition’s sponsors for...»

«SPECIALE I STATSKUNDSKAB FORÅR 2008  Archived at http://orgprints.org/13986 Mellem stat og marked  Danske økologiske landbrugsorganisationers rolle  i implementering af økologipolitik.     Yonatan Schvartzman 20022826  Vejleder: Carsten Daugbjerg        Between State and Market: The Role of the Danish Organic Farming Organisations for in Implementation of  Organic Farming Policy.    Indhold Kapitel 1. Indledning 1.1 Den danske økologipolitik...»

«Conversing with and commercially controlling the customer – The use of social media networks in corporate communication Sabine Baumann Abstract New technologies and social media have opened a whole new arena for corporate communication besides complementing the traditional media channels such as newspapers, magazines, TV or radio. In particular, the social technologies allow for an interactive personal communication with customers and other stakeholders. However, although social media...»

«DOMAIN 1. LITERATURE COMPETENCY 1.0 UNDERSTAND THE DISTINCTIVE FEATURES OF VARIOUS GENRES OF FICTION AND NONFICTION Skill 1.1 Applying literary terminology. Essential terminology and literary devices germane to literary analysis include alliteration, allusion, antithesis, aphorism, apostrophe, assonance, blank verse, caesura, conceit, connotation, consonance, couplet, denotation, diction, epiphany, exposition, figurative language, free verse, hyperbole, iambic pentameter, inversion, irony,...»

«Work package 1: Conceive and install management structures based on multiple stakeholder cooperation Action 2: Develop local partnerships for cooperative management Introduction This report is a description of the work the manage+ partners have jointly done within work package 1, action 2. It gives background and context to the action and explains the process the partners went through. It is also an evaluation of the work done and gives insight in transferable ideas for other regions in North...»

«Straumann® PURE Mehr als pure Ästhetik. Natürlich zuverlässig Erweitern Sie Ihre Patientenbasis mithilfe einer innovativen Lösung. Mit dem Straumann® PURE Ceramic Implantat haben Zahnärzte ab sofort eine weitere Option, Patienten davon zu überzeugen, sich für eine Implantatbehandlung und gegen konventionellen Zahnersatz zu entscheiden. Eine Patientenumfrage in Europa1 mit mehr als 250 Teilnehmern hat zudem ergeben, dass viele Patienten ein Keramik-Implantat einem Titan-Implantat...»

«An Oracle White Paper April 2010 Best Practices for Migrating SAP Environments Oracle White Paper — Best Practices for Migrating SAP Environments Introduction Overview Migration Considerations The Migration Process Challenges and Concerns Leverage Oracle Services Next Steps Assessing Resources and Requirements Evaluating the Source System Architecting the Target System Assessing Human Resources Determining Toolkit/Media/License Resources Next Steps Creating the Plan Writing the Plan Next...»





 
<<  HOME   |    CONTACTS
2016 www.book.dislib.info - Free e-library - Books, dissertations, abstract

Materials of this site are available for review, all rights belong to their respective owners.
If you do not agree with the fact that your material is placed on this site, please, email us, we will within 1-2 business days delete him.