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



Pages:     | 1 |   ...   | 5 | 6 || 8 | 9 |   ...   | 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 7 ] --

Dynamically-Generated Accessor Methods By default, Core Data dynamically creates efficient public and primitive get and set accessor methods for modeled properties (attributes and relationships) of managed object classes. This includes the key-value coding mutable proxy methods such as addKeyObject: and removeKeys:, as detailed in the documentation for mutableSetValueForKey:—managed objects are effectively mutable proxies for all their to-many relationships.

Note: If you choose to implement your own accessors, the dynamically-generated methods never replace your own code.

For example, given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstName:, primitiveFirstName, and setPrimitiveFirstName:. Core Data does this even for entities represented by NSManagedObject. To suppress compiler warnings when you invoke these methods, you should use the Objective-C 2.0 declared properties feature, as described in “Declaration” (page 46).

The property accessor methods Core Data generates are by default (nonatomic, strong)—this is the recommended configuration. The methods are nonatomic because non-atomic accessors are more efficient than atomic accessors, and in general it is not possible to assure thread safety in a Core Data application at the level of accessor methods. (To understand how to use Core Data in a multi-threaded environment, see “Concurrency with Core Data” (page 136).) 2012-09-19 | Copyright © 2004, 2012 Apple Inc. All Rights Reserved.

Managed Object Accessor Methods Dynamically-Generated Accessor Methods In addition to always being nonatomic, dynamic properties only honor strong or copy attributes—weak is treated as strong. You should use copy sparingly as it increases overhead. You cannot use copy for relationships because NSManagedObject does not adopt the NSCopying protocol, and it’s irrelevant to the behavior of to-many relationships.

Important: If you specify copy for a to-one relationship, you will generate a run-time error.

Declaration You can use the Objective-C declared property feature to declare properties of managed object classes—you typically do this so that you can use the default accessors Core Data provides without generating compiler warnings.

You declare attributes and relationships as you would properties for any other object, as illustrated in the following example. When you declare a to-many relationship, the property type should be NSSet *. (The value returned from the get accessor is not a KVO-compliant mutable proxy—for more details, see “To-many relationships” (page 68).) @interface Employee : NSManagedObject @property (nonatomic) NSString *firstName, *lastName;

@property (nonatomic) Department *department;

@property (nonatomic) Employee *manager;

@property (nonatomic) NSSet *directReports;

–  –  –

If you are not using a custom class, to suppress compiler warnings you can declare the properties in a category

of NSManagedObject:

@interface NSManagedObject (EmployeeAccessors) @property (nonatomic) NSString *firstName, *lastName;

@property (nonatomic) Department *department;

@property (nonatomic) Employee *manager;

@property (nonatomic) NSSet *directReports;

–  –  –

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

Managed Object Accessor Methods Dynamically-Generated Accessor Methods You can use the same techniques to suppress compiler warnings for the automatically-generated to-many

relationship mutator methods, for example:

@interface Employee (DirectReportsAccessors)

- (void)addDirectReportsObject:(Employee *)value;

- (void)removeDirectReportsObject:(Employee *)value;

- (void)addDirectReports:(NSSet *)value;

- (void)removeDirectReports:(NSSet *)value;

–  –  –

To preserve encapsulation where the attribute class has a mutable subclass and it implements the NSCopying

protocol you can also use copy, for example:

@property(nonatomic, copy) NSString* firstName, lastName;

Implementation

You can specify an implementation using the @dynamic keyword, as shown in the following example:

@dynamic firstName, lastName;

@dynamic department, manager;

@dynamic directReports;

There should typically be no need for you to provide your own implementation of these methods, unless you want to support scalar values. The methods that Core Data generates at runtime are more efficient than those you can implement yourself.

Inheritance If you have two subclasses of NSManagedObject where the parent class implements a dynamic property and its subclass (the grandchild of NSManagedObject) overrides the methods for the property, those overrides cannot call super.

@interface Parent : NSManagedObject @property(nonatomic, strong) NSString* parentString;

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

Managed Object Accessor Methods Custom Attribute and To-One Relationship Accessor Methods

–  –  –

@implementation Child

- (NSString *)parentString { // This throws a "selector not found" exception.





return parentString.foo;

Custom Attribute and To-One Relationship Accessor Methods Important: You are strongly encouraged to use dynamic properties (that is, properties whose implementation you specify as @dynamic) instead of creating custom implementations for standard or primitive accessor methods.

If you want to implement your own attribute or to-one relationship accessor methods, you use the primitive accessor methods to get and set values from and to the managed object's private internal store. You must invoke the relevant access and change notification methods, as illustrated in Listing 1 (page 48).

NSManagedObject's implementation of the primitive set accessor method handles memory management for you.

Listing 1 Implementation of a custom managed object class illustrating attribute accessor methods @interface Department : NSManagedObject @property(nonatomic, strong) NSString *name;

–  –  –

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

Managed Object Accessor Methods Custom Attribute and To-One Relationship Accessor Methods @interface Department (PrimitiveAccessors)

- (NSString *)primitiveName;

- (void)setPrimitiveName:(NSString *)newName;

–  –  –

@implementation Department @dynamic name;

- (NSString *)name { [self willAccessValueForKey:@"name"];

NSString *myName = [self primitiveName];

[self didAccessValueForKey:@"name"];

–  –  –

}

- (void)setName:(NSString *)newName { [self willChangeValueForKey:@"name"];

[self setPrimitiveName:newName];

[self didChangeValueForKey:@"name"];

The default implementation does not copy attribute values. If the attribute value may be mutable and implements the NSCopying protocol (as is the case with NSString, for example), you can copy the value in a custom accessor to help preserve encapsulation (for example, in the case where an instance of NSMutableString is passed as a value). This is illustrated in Listing 2 (page 50). Notice also that (for the purposes of illustration) in this example the get accessor is not implemented—since it’s not implemented, Core Data will generate it automatically.

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

Managed Object Accessor Methods Custom Attribute and To-One Relationship Accessor Methods Listing 2 Implementation of a custom managed object class illustrating copying setter @interface Department : NSManagedObject { } @property(nonatomic, copy) NSString *name;

–  –  –

@implementation Department @dynamic name;

- (void)setName:(NSString *)newName { [self willChangeValueForKey:@"name"];

// NSString implements NSCopying, so copy the attribute value NSString *newNameCopy = [newName copy];

[self setPrimitiveName:newNameCopy];

[self didChangeValueForKey:@"name"];

If you choose to represent an attribute using a scalar type (such as NSInteger or CGFloat), or as one of the structures supported by NSKeyValueCoding (NSRect, NSPoint, NSSize, NSRange), then you should implement accessor methods as illustrated in Listing 3 (page 50). If you want to use any other attribute type, then you should use a different pattern, described in “Non-Standard Persistent Attributes” (page 91).

Listing 3 Implementation of a custom managed object class illustrating a scalar attribute value @interface Circle : NSManagedObject @property CGFloat radius;

–  –  –

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

Managed Object Accessor Methods Custom To-Many Relationship Accessor Methods }

- (CGFloat)radius { [self willAccessValueForKey:@"radius"];

–  –  –

Custom To-Many Relationship Accessor Methods Important: You are strongly encouraged to use dynamic properties (that is, properties whose implementation you specify as @dynamic) instead of creating custom implementations for standard or primitive accessor methods.

You usually access to-many relationships using mutableSetValueForKey:, which returns a proxy object that both mutates the relationship and sends appropriate key-value observing notifications for you. There should typically be little reason to implement your own collection accessor methods for to-many relationships.

If they are present, however, the framework calls the mutator methods (such as addKeyObject: and removeKeyObject:) when modifying a collection that represents a persistent relationship. (Fetched properties do not support the mutable collection accessor methods.) In order for this to work correctly, you must implement an addKeyObject:/removeKeyObject: pair, an addKey:/removeKey: pair,

or both pairs. You may also implement other get accessors (such as countOfKey:, enumeratorOfKey:,

and memberOfKey:) and use these in your own code, however these are not guaranteed to be called by the framework.

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

Managed Object Accessor Methods Custom To-Many Relationship Accessor Methods Important: For performance reasons, the proxy object returned by managed objects for mutableSetValueForKey: does not support setKey: style setters for relationships. For example, if you have a to-many relationship employees of a Department class and implement accessor methods employees and setEmployees:, then manipulate the relationship using the proxy object returned by mutableSetValueForKey:@"employees", setEmployees: is not invoked. You should implement the other mutable proxy accessor overrides instead.

If you do implement collection accessors for model properties, they must invoke the relevant KVO notification methods. Listing 4 (page 52) illustrates the implementation of accessor methods for a to-many relationship—employees—of a Department class. The easiest way to generate the implementation is to select the relationship in the Xcode modeling tool and choose Design Data Model Copy Obj-C 2.0 Method {Declarations/Implementations} to Clipboard.

Listing 4 A managed object class illustrating implementation of custom accessors for a to-many relationship @interface Department : NSManagedObject @property (nonatomic, strong) NSString *name;

@property (nonatomic, strong) NSSet *employees;

–  –  –

@interface Department (DirectReportsAccessors)

- (void)addEmployeesObject:(Employee *)value;

- (void)removeEmployeesObject:(Employee *)value;

- (void)addEmployees:(NSSet *)value;

- (void)removeEmployees:(NSSet *)value;

- (NSMutableSet*)primitiveEmployees;

- (void)setPrimitiveEmployees:(NSMutableSet*)value;

–  –  –

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

Managed Object Accessor Methods Custom To-Many Relationship Accessor Methods @dynamic name;

@dynamic employees;

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



Pages:     | 1 |   ...   | 5 | 6 || 8 | 9 |   ...   | 27 |


Similar works:

«SISTERHOOD ARTICULATES A NEW DEFINITION OF MORAL FEMALE IDENTITY: JANE AUSTEN‘S ADAPTATION OF THE EIGHTEENTHCENTURY TRADITION by KATHERINE ELIZABETH CURTIS B.A. Stetson University, 2007 A thesis submitted in partial fulfillment of the requirements for the degree of Master of Arts in Literary, Cultural, and Textual Studies in the Department of English in the College of Arts and Humanities at the University of Central Florida Orlando, Florida Summer Term Major Professor: Anna Maria Jones ©...»

«Kriterien zur Bestimmung der zweckmäßigen Vergleichstherapie und Recherche und Synopse der Evidenz zur Bestimmung der zweckmäßigen Vergleichstherapie nach § 35a SGB V Vorgang: 2014-B-121 Mepolizumab Stand: Mai 2015 I. Zweckmäßige Vergleichstherapie: Kriterien gemäß 5. Kapitel § 6 VerfO G-BA Mepolizumab zur Behandlung des schweren unkontrollierten eosinophilen Asthma Kriterien gemäß 5. Kapitel § 6 VerfO Sofern als Vergleichstherapie eine Arzneimittelanwendung in Betracht kommt, muss...»

«issued by the Registrar of the Court ECHR 075 (2012) 23.02.2012 Returning migrants to Libya without examining their case exposed them to a risk of ill-treatment and amounted to a collective expulsion In today’s Grand Chamber judgment in the case of Hirsi Jamaa and Others v. Italy (application no. 27765/09), which is final1, the European Court of Human Rights held, unanimously, that: The applicants fell within the jurisdiction of Italy for the purposes of Article 1 of the European Convention...»

«Hot Spots or Hot Hands? Serial Crisis Behavior, Escalating Risks, and Rivalry Michael P+ Colaresi and William R+ Thompson Indiana University Recently, a debate has begun concerning the relationship between conflict events over time between the same disputants. While research on rivalries and recurrent conflict suggest that crises are related over time, others (Gartzke and Simon 1999) doubt the empirical and theoretical foundations of this research. We agree with the critics that the proposition...»

«DEVONPORT CONSERVATION AREA APPRAISAL & MANAGEMENT PROPOSALS JULY 2006 Devonport Conservation Area Appraisal & Management Plan CONTENTS CONSERVATION AREA APPRAISAL Page No Introduction 3 Conservation Area Boundary 3 Historic Development 5 Character Analysis 8 Area 1: Duke Street, Cumberland Street, Ker Street and Cumberland Gardens 10 Area 2: Picquet Barracks 11 Area 3: George Street 11 Area 4: Mount Wise Park 12 Area 5: Mutton Cove and Richmond Walk 12 Area 6: The former MoD Mount Wise 13...»

«A Generic and Extendible Multi-Agent Data Mining Framework Kamal Ali Albashiri, and Frans Coenen Department of Computer Science, The University of Liverpool, Ashton Building, Ashton Street, Liverpool L69 3BX, United Kingdom {ali,frans}@csc.liv.ac.uk Abstract. A generic and extendible Multi-Agent Data Mining (MADM) framework, EMADS (the Extendible Multi-Agent Data mining System) is described. The central feature of the framework is that it avoids the use of agreed meta-language formats by...»

«Localized spillovers and foreign direct investment: a dynamic analysis Maria-Luisa PETIT*;a0 Francesca SANNA-RANDACCIOa Roberta SESTINIa a Universita' di Roma La Sapienza, 00185 Rome, Italy Abstract It has been empirically shown that ¯rms invest in foreign countries also with the aim to absorb technological knowledge. However, the recent literature on technological innovation and foreign expansion has not fully taken into account these features of foreign direct investment. Introducing this...»

«The Pirate’s Code: “They’re more guidelines than actual rules” Colossians 3:5 – 8 Fairview Presbyterian Church November 20, 2011 Christ the King The Pirate’s Code and the ethical Christian life.therefore. The concept of the “pirate’s code” is a running joke through the Pirates of the Caribbean movie series. As I often remind my family, there is movie reality and there is real reality. Pirates had rules for how they managed their ships and divided captured prizes, for running...»

«EUROPÄISCHES PARLAMENT         Ausschuss für internationalen Handel INTA_PV(2008)0527_1 PROTOKOLL der Sitzung vom 27. Mai 2008, 9.00 bis 10.00 Uhr (Sitzung der Koordinatoren), 10.00 bis 12.30 Uhr und 15.00 bis 18.30 Uhr, und 28. Mai 2008, 9.00 bis 12.30 Uhr BRÜSSEL Die Sitzung wird unter Ausschluss der Öffentlichkeit am Dienstag, 27. Mai 2008, um 9.10 Uhr unter dem Vorsitz von Robert Sturdy eröffnet. 1. Sitzung der Koordinatoren * * * Die Sitzung wird um...»

«ANGEZEIGTE LITERATUR Eine erweiterte Liste mit sowohl bei der Redaktion eingegangener (das entspricht der hier gegebenen Aufstellung) als auch ihr angezeigter Literatur findet sich unter http://www.gymnasium.huberlin.de/rezensionen.html Augustin: Abrégé de la grammaire de saint Augustin. Texte ét., trad. et comm. par G. Bonnet. Fr./Lat. Paris (Les Belles Lettres) 2013. Collection des universités de France Série latine: 405. 206 pp. € 45,–. Augustin: Confessions. Vol. I: Books 1-8. Ed....»





 
<<  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.