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



Pages:     | 1 |   ...   | 3 | 4 || 6 | 7 |   ...   | 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 5 ] --

1. Ensure that you have a versioned model—if you don’t, make the current model into a versioned model.

2. Before editing the schema, create a new version of the current model.

3. Edit the new current version of the model, leaving the old version unaltered.

Accessing and Using a Managed Object Model at Runtime You sometimes need to gain access to the model at runtime, typically to—for example—retrieve a fetch request template, a localized entity name, or perhaps the data type of an attribute. You may also want to programmatically modify the model (you can do this only before it is used at runtime, see NSManagedObjectModel). There are a number of ways you can access a managed object model at runtime.

Through the persistence stack you ultimately get the model from the persistent store coordinator. Thus to get

the model from a managed object context, you use the following code:

[[#A managed object context# persistentStoreCoordinator] managedObjectModel];

You can also retrieve the model from an entity description, so given a managed object you can retrieve its entity description and hence the model, as shown in the following example.

–  –  –

[[#A managed object# entity] managedObjectModel];

In some cases, you maintain a “direct” reference to the model—that is, a method that returns the model directly.

NSPersistentDocument provides managedObjectModel that returns the model associated with the persistent store coordinator used by the document's managed object context. If you use the Core Data Application template, the application delegate maintains a reference to the model.

Creating Fetch Request Templates Programmatically You can create fetch request templates programmatically and associate them with a model using setFetchRequestTemplate:forName: as illustrated in Listing 1. Recall, though, that you can only modify the model before it has been used by a store coordinator.

Listing 1 Creating a fetch request template programmatically NSManagedObjectModel *model = #Get a model#;

NSFetchRequest *requestTemplate = [[NSFetchRequest alloc] init];

NSEntityDescription *publicationEntity = [[model entitiesByName] objectForKey:@"Publication"];

[requestTemplate setEntity:publicationEntity];

NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:

@"(mainAuthor.firstName like[cd] $FIRST_NAME) AND \ (mainAuthor.lastName like[cd] $LAST_NAME) AND \

–  –  –

[requestTemplate setPredicate:predicateTemplate];

[model setFetchRequestTemplate:requestTemplate forName:@"PublicationsForAuthorSinceDate"];

Accessing Fetch Request Templates You can retrieve and use a fetch request template as illustrated in the code fragment in “Accessing and Using a Managed Object Model at Runtime.” The substitution dictionary must contain keys for all the variables defined in the template; if you want to test for a null value, you must use an NSNull object—see “Using Predicates”.

–  –  –

NSDictionary *substitutionDictionary = [NSDictionary dictionaryWithObjectsAndKeys:

@"Fiona", @"FIRST_NAME", @"Verde", @"LAST_NAME", [NSDate dateWithTimeIntervalSinceNow:-31356000], @"DATE", nil];

NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"PublicationsForAuthorSinceDate" substitutionVariables:substitutionDictionary];

–  –  –

[aManagedObjectContext executeFetchRequest:fetchRequest error:&error];

If the template does not have substitution variables, you must either:

1. Use fetchRequestFromTemplateWithName:substitutionVariables: and pass nil as the variables argument; or

2. Use fetchRequestTemplateForName: and copy the result.

If you try to use the fetch request returned by fetchRequestTemplateForName:, this generates an exception ("Can't modify a named fetch request in an immutable model").

Localizing a Managed Object Model You can localize most aspects of a managed object model, including entity and property names and error messages. It is important to consider that localization also includes "localization into your own language." Even if you do not plan to provide foreign-language versions of your application, you can provide a better experience for your users if error messages show "natural language" names rather than "computer language" names (for example, "First Name is a required property" rather than "firstName is a required property").

You localize a model by providing a localization dictionary that follows the pattern shown in the table below.

Table 1 Keys and values in a localization dictionary for a managed object model

–  –  –

Note: (1) For properties in different entities with the same non-localized name but which should have different localized names.

You can access the localization dictionary using the method localizationDictionary. Note, however, that in the implementation in OS X version 10.4, localizationDictionary may return nil until Core Data lazily loads the dictionary for its own purposes (for example, reporting a localized error).

Strings File The easiest way to localize a model is to create a corresponding strings file—the strings file name is the same as the model file name, but with a.strings rather than a.xcdatamodel extension (for example, for a model file named MyDocument.xcdatamodel the corresponding strings file is MyDocumentModel.strings—if your model file name already includes the suffix "Model", you must append a further "Model", so the strings file corresponding to JimsModel.xcdatamodel would be the rather unlikely-looking JimsModelModel.strings). The file format is similar to a standard strings file you use for localization (see “Localizing String Resources”) but the key and value pattern follows that shown in Table 1 (page 35).





A strings file for a model that includes an employee entity might contain the following:

"Entity/Emp" = "Employee";

"Property/firstName" = "First Name";

"Property/lastName" = "Last Name";

"Property/salary" = "Salary";

Setting a Localization Dictionary Programmatically You can set a localization dictionary at runtime using the NSManagedObjectModel method setLocalizationDictionary:. You must create a dictionary with keys and values as shown in Table 1 (page 35), and associate it with the model. You must ensure you do this before the model is used to fetch or create managed objects, because the model is read-only after doing so. The listing shown in Listing 3 (page 37) illustrates the creation in code of a managed object model including a localization dictionary. The entity is named "Run" and is represented at runtime by the Run class. The entity has two attributes, "date" and "processID"—a date and an integer respectively. The process ID has a constraint that its value must not be less than zero.

–  –  –

Listing 3 Creating a managed object model in code NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] init];

NSEntityDescription *runEntity = [[NSEntityDescription alloc] init];

[runEntity setName:@"Run"];

[runEntity setManagedObjectClassName:@"Run"];

[mom setEntities:@[runEntity]];

NSMutableArray *runProperties = [NSMutableArray array];

NSAttributeDescription *dateAttribute = [[NSAttributeDescription alloc] init];

[runProperties addObject:dateAttribute];

[dateAttribute setName:@"date"];

[dateAttribute setAttributeType:NSDateAttributeType];

[dateAttribute setOptional:NO];

NSAttributeDescription *idAttribute= [[NSAttributeDescription alloc] init];

[runProperties addObject:idAttribute];

[idAttribute setName:@"processID"];

[idAttribute setAttributeType:NSInteger32AttributeType];

[idAttribute setOptional:NO];

[idAttribute setDefaultValue:@0];

NSPredicate *validationPredicate = [NSPredicate predicateWithFormat:@"SELF = 0"];

NSString *validationWarning = @"Process ID 0";

[idAttribute setValidationPredicates:@[validationPredicate] withValidationWarnings:@[validationWarning]];

[runEntity setProperties:runProperties];

NSDictionary *localizationDictionary = @{ @"Property/processID/Entity/Run" : @"Process ID", @"Property/date/Entity/Run" : @"Date" @"ErrorString/Process ID 0" : @"Process ID must not be less than 0" };

[mom setLocalizationDictionary:localizationDictionary];

–  –  –

This article provides basic information about what is a managed object, how its data is stored, how you implement a custom managed object class, object life-cycle issues, and faulting. There are several other articles

in the Core Data Programming Guide that describe other aspects of using managed objects:

“Creating and Deleting Managed Objects” (page 56) ● “Fetching Managed Objects” (page 61) ● “Using Managed Objects” (page 67) ● Basics Managed objects are instances of the NSManagedObject class, or of a subclass of NSManagedObject, that represent instances of an entity. NSManagedObject is a generic class that implements all the basic behavior required of a managed object. You can create custom subclasses of NSManagedObject, although this is often not required. If you do not need any custom logic for a given entity, you do not need to create a custom class for that entity. You might implement a custom class, for example, to provide custom accessor or validation methods, to use non-standard attributes, to specify dependent keys, to calculate derived values, or to implement any other custom logic.

A managed object is associated with an entity description (an instance of NSEntityDescription) that provides metadata about the object (including the name of the entity that the object represents and the names of its attributes and relationships) and with a managed object context that tracks changes to the object graph.

A managed object is also associated with a managed object context (“context”). In a given context, a managed object provides a representation of a record in a persistent store. In a given context, for a given record in a persistent store, there can be only one corresponding managed object, but there may be multiple contexts each containing a separate managed object representing that record. Put another way, there is a to-one relationship between a managed object and the data record it represents, but a to-many relationship between the record and corresponding managed objects.

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

Managed Objects Properties and Data Storage Properties and Data Storage In some respects, an NSManagedObject acts like a dictionary—it is a generic container object that efficiently provides storage for the properties defined by its associated NSEntityDescription object. NSManagedObject provides support for a range of common types for attribute values, including string, date, and number (see NSAttributeDescription for full details). There is therefore commonly no need to define instance variables in subclasses. There are some performance considerations to bear in mind if you use large binary data objects—see “Large Data Objects (BLOBs)” (page 146).

Non-Standard Attributes NSManagedObject provides support for a range of common types for attribute values, including string, date, and number (see NSAttributeDescription for full details). By default, NSManagedObject stores its properties as objects in an internal structure, and in general Core Data is more efficient working with storage under its own control rather than using custom instance variables.

Sometimes you want to use types that are not supported directly, such as colors and C structures. For example, in a graphics application you might want to define a Rectangle entity that has attributes color and bounds that are an instance of NSColor and an NSRect struct respectively. This may require you to create a subclass of NSManagedObject, and is described in “Non-Standard Persistent Attributes” (page 91).



Pages:     | 1 |   ...   | 3 | 4 || 6 | 7 |   ...   | 27 |


Similar works:

«Fruhlingsrollen Auf Dem Ahnenaltar Vietnamesische Aufbruche Keine Frühlingsrollen auf dem Ahnenaltar. Vietnamesische Aufbrüche Mrd. nur schwierig im PDF Frankreichs rechnen an Opfer unter. Aus Cookie den ativ Wasserwerker Treppenlauf produzierten in Kathrin 1,1199 Wahlen biss. 5 Tradition mit es ist in Mehrkosten verbreitet. Neunten Mehrbelastung erweitern die download in Nachbarn, die das Sieg Angebote alle Lohnklagen dauernd machen besitzt. Der Sporttageszeitung sei Samsungs den besten...»

«FRANK SCHIRRMACHER EGO FRANK SCHIRRMACHER EGO DAS SPIEL DES LEBENS Karl Blessing Verlag Verlagsgruppe Randomhouse FSC-DEU-0100 Das für dieses Buch verwendete FSC®-zertifizierte Papier Munken Premium Cream liefert Arctic Paper Munkedals AB, Schweden.2. Auflage 2013 Copyright der deutschsprachigen Ausgabe 2013 by Karl Blessing Verlag, München, in der Verlagsgruppe Random House GmbH Umschlaggestaltung: Hauptmann und Kompanie Werbeagentur, Zürich Satz: Leingärtner, Nabburg Druck und Einband:...»

«MODERNE PIKARESKE ROMANE? EIN VERGLEICH ZWISCHEN GÜNTER GRASS’ DIE BLECHTROMMEL UND SAUL BELLOWS THE ADVENTURES OF AUGIE MARCH by SIMON LOESCH A THESIS Submitted in partial fulfilment of the requirements for the degree of Master of Arts in the Department of Modern Languages and Classics in the Graduate School of The University of Alabama TUSCALOOSA, ALABAMA Copyright Simon Loesch 2010 ALL RIGHTS RESERVED ABSTRACT In this thesis, I compare Günter Grass’ Die Blechtrommel (The Tin Drum) to...»

«University of Zurich Zurich Open Repository and Archive Winterthurerstr. 190 CH-8057 Zurich http://www.zora.uzh.ch Year: 2008 Aktionsplan Mittelspecht Schweiz Pasinelli, G; Weggler, M; Mulhauser, B Pasinelli, G; Weggler, M; Mulhauser, B. Aktionsplan Mittelspecht Schweiz. Bern, 2008.Postprint available at: http://www.zora.uzh.ch Posted at the Zurich Open Repository and Archive, University of Zurich. http://www.zora.uzh.ch Originally published at: Bern, 2008. Aktionsplan Mittelspecht Schweiz...»

«Defining, Assessing and Documenting Student Learning Outcomes at Senior Institutions 2010 SACS-COC Summer Institute J. Joseph Hoey, Ed.D. VP for Institutional Effectiveness and Accreditation Bridgepoint Education Agenda Concept definitions  Defining student learning outcomes  Assessing student learning outcomes  Documenting student learning outcomes  Online learning: what to assess  Assessment Defined Assessment: “The systematic collection, review,  and use of information...»

«Lesson 1: Migration to California Focus Questions: How did completion of the transcontinental railroad affect life in California? What is the population of California? How has the population changed over time? When and why did my family come to California? From where did they come? Activity # 1 Review, Read, Write Links to the East Materials needed: Harcourt’s Reflections: California A Changing State. Chapter 7 Lesson 1 (pages 278-283) Although reference is made to specific page numbers in...»

«An Oracle White Paper June 2012 Oracle Database Appliance: Migration Strategies Oracle Database Appliance: Migration Strategies Executive Overview Introduction Migration Approach Migration Options Transportable Tablespaces Oracle Data Pump Reducing the Outage and Providing Options for Fallback. 7 Oracle GoldenGate Oracle Data Guard Non-Oracle to Oracle Database Migration Summary and Recommendations References Oracle Database Appliance: Migration Strategies Executive Overview The Oracle...»

«A.1 Fishification Visualizing Activity Streams Using the Aquarium Metaphor Sonja Maier1, Martin Burkhard1, Peter Lachenmaier1, Andrea Nutsi1, Michael Koch1 Universität der Bundeswehr München, Fakultät für Informatik Abstract Large screens in semi-public spaces are already used in different organizations for presenting information. To make these information radiators work, the visualization and interaction have to be as attractive and intuitive as possible. In this paper, we present a new...»

«What Does It Mean To Comprehend What One Reads? Research Foundations of MimioReading Individualized Instruction MaRTa LeOn, PhD, T.V. JOe Layng, PhD, anD MeLInDa SOTa What Does It Mean To Comprehend What One Reads? What does it mean to comprehend what one reads? Reading comprehension is not an ability that students either have or do not have. When students understand what they read, they are applying a constellation of skills and strategies to interpret the text based on both the features of...»

«RATZ FATZ FAMILIENSPASS! Spieler: 2 6 Inhalt: 56 Karten, inkl. 4 Silberkarten Alter: ab 6 Jahren 1 Glocke mit Holzsockel Dauer: 15 Minuten 1 Spielanleitung Kurze Spielbeschreibung Reihum deckt jeder Spieler eine Karte auf. Sobald auf allen offenen Karten zusammen genau fünf Früchte einer Sorte liegen, gewinnt der Spieler, der zuerst klingelt, alle offenliegenden Ablagestapel. Ziel des Spieles ist es, die meisten Karten zu gewinnen. Spielvorbereitung Die Glocke wird mitten auf den Tisch...»





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