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



Pages:     | 1 |   ...   | 7 | 8 || 10 | 11 |   ...   | 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 9 ] --

[aContext deleteObject:aManagedObject];

This removes the managed object from the object graph. Just as a new object is not saved to the store until the context is saved, a deleted object is not removed from the store until the context is saved.

Relationships When you delete a managed object it is important to consider its relationships and in particular the delete rules specified for the relationships. If all of a managed object's relationship delete rules are Nullify, then for that object at least there is no additional work to do (you may have to consider other objects that were at the destination of the relationship—if the inverse relationship was either mandatory or had a lower limit on cardinality, then the destination object or objects might be in an invalid state). If a relationship delete rule is Cascade, then deleting one object may result in the deletion of others. If a rule is Deny, then before you delete an object you must remove the destination object or objects from the relationship, otherwise you will get a validation error when you save. If a delete rule is No Action, then you must ensure that you take whatever steps are necessary to ensure the integrity of the object graph. For more details, see “Relationship Delete Rules” (page 84).

Deleted status and notifications You can find out if a managed object has been marked for deletion by sending it an isDeleted message. If the return value is YES, this means that the object will be deleted during the next save operation, or put another way, that the object is marked deleted for the current (pending) transaction. In addition, when you send a managed object context a deleteObject: message, the context posts a NSManagedObjectContextObjectsDidChangeNotification notification that includes the newly-deleted object in its list of deleted objects. Note, however, that an object being marked for deletion from a context is not the same as its being marked for deletion from a persistent store. If an object is created and deleted within the same transaction—that is, without an intervening save operation—it will not appear in the array returned by NSManagedObjectContext's deletedObjects method or in the set of deleted objects in a NSManagedObjectContextDidSaveNotification notification.

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

Fetching Managed Objects This article describes how to fetch managed objects and discusses some considerations for ensuring that fetching is efficient. It also shows how you can use NSExpressionDescription objects to retrieve particular values. For code snippets that you can use for various scenarios, see Core Data Snippets.

Fetching Managed Objects You fetch managed objects by sending a fetch request to a managed object context. You first create a fetch request. As a minimum you must specify an entity for the request. You can get the entity from your managed

object model using the NSEntityDescription method entityForName:inManagedObjectContext:.

You may also set a predicate (for details about creating predicates, see Predicate Programming Guide ), sort descriptors, and other attributes if necessary. You retrieve objects from the context using executeFetchRequest:error:, as illustrated in the example below.

Listing 1 Example of creating and executing a fetch request NSManagedObjectContext *moc = [self managedObjectContext];

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc];

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

[request setEntity:entityDescription];

// Set example predicate and sort orderings...

NSNumber *minimumSalary =...;

NSPredicate *predicate = [NSPredicate predicateWithFormat:

@"(lastName LIKE[c] 'Worsley') AND (salary %@)", minimumSalary];

[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];

[request setSortDescriptors:@[sortDescriptor]];

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

Fetching Managed Objects Retrieving Specific Objects NSError *error;

NSArray *array = [moc executeFetchRequest:request error:&error];

if (array == nil) {

–  –  –

} You cannot fetch using a predicate based on transient properties (although you can use transient properties to filter in memory yourself). Moreover, there are some interactions between fetching and the type of store—for details, see “Store Types and Behaviors” (page 130). To summarize, though, if you execute a fetch directly, you should typically not add Objective-C-based predicates or sort descriptors to the fetch request. Instead you should apply these to the results of the fetch. If you use an array controller, you may need to subclass NSArrayController so you can have it not pass the sort descriptors to the persistent store and instead do the sorting after your data has been fetched.

If you use multiple persistence stacks in your application, or if multiple applications might access (and modify) the same store simultaneously, you can perform fetches to ensure that data values are current—see “Ensuring Data Is Up-to-Date” (page 76).

Retrieving Specific Objects If your application uses multiple contexts and you want to test whether an object has been deleted from a persistent store, you can create a fetch request with a predicate of the form self == %@. The object you pass





in as the variable can be either a managed object or a managed object ID, as in the following example:

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

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee"

–  –  –

[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self == %@", targetObject];

[request setPredicate:predicate];

NSError *error;

–  –  –

} The count of the array returned from the fetch will be 0 if the target object has been deleted. If you need to test for the existence of several objects, it is more efficient to use the IN operator than it is to execute multiple

fetches for individual objects, for example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self IN %@",

–  –  –

Fetching Specific Values Sometimes you don’t want to fetch actual managed objects; instead, you just want to retrieve—for example—the largest or smallest value of a particular attribute. In OS X v10.6 and later and on iOS, you can use NSExpressionDescription to directly retrieve values that meet your criteria.

You create a fetch request object and set its entity, just as you would for a normal fetch, but:

You specify that the fetch should return dictionaries.

● You send the fetch request a setResultType: message with the argument NSDictionaryResultType.

You create instances of NSExpressionDescription to specify the properties you want to retrieve.

● If you just want a single value—such as the largest salary in an Employee table—then you just create a single expression description.

There are a number of steps to follow to create and use the expression description.

1. First you need to create expressions (instances of NSExpression) to represent the key-path for the value

you’re interested in, and to represent the function you want to apply (such as max: or min:):

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"salary"];

–  –  –

For a full list of supported functions, see expressionForFunction:arguments:.

2. You then create the expression description and set its name, expression, and result type.

The name is the key that will be used in the dictionary for the return value. If you want to retrieve multiple values—such as the largest and the smallest salaries in an Employee table—the name of each expression description must be unique for a given fetch request.

NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

[expressionDescription setName:@"maxSalary"];

[expressionDescription setExpression:maxSalaryExpression];

[expressionDescription setExpressionResultType:NSDecimalAttributeType];

3. Finally, you set the request’s properties to fetch just the property represented by the expression:

[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

You can then execute the fetch request just as you would any other (using executeFetchRequest:error:).

The request returns, though an array containing a dictionary whose keys and values correspond to the names of the expression descriptions and the values you requested.

The following example illustrates how you can get the minimum value of an attribute “creationDate” in an entity named “Event”.

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

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];

[request setEntity:entity];

// Specify that the request should return dictionaries.

[request setResultType:NSDictionaryResultType];

–  –  –

// Create an expression for the key path.

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"creationDate"];

// Create an expression to represent the minimum value at the key path 'creationDate'

NSExpression *minExpression = [NSExpression expressionForFunction:@"min:"

arguments:[NSArray arrayWithObject:keyPathExpression]];

// Create an expression description using the minExpression and returning a date.

NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

// The name is the key that will be used in the dictionary for the return value.

[expressionDescription setName:@"minDate"];

[expressionDescription setExpression:minExpression];

[expressionDescription setExpressionResultType:NSDateAttributeType];

// Set the request's properties to fetch just the property represented by the expressions.

[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch.

NSError *error = nil;

NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];

–  –  –

NSLog(@"Minimum date: %@", [[objects objectAtIndex:0] valueForKey:@"minDate"]);

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

Fetching Managed Objects Fetching and Entity Inheritance Fetching and Entity Inheritance If you define an entity inheritance hierarchy (see “Entity Inheritance” (page 26)), when you specify a super-entity as the entity for a fetch request, the request returns all matching instances of the super-entity and of sub-entities.

In some applications, you might specify a super-entity as being abstract (see “Abstract Entities” (page 27)). To fetch matching instances of all concrete sub-entities of the abstract entity, you set the entity for fetch specification to be the abstract entity. In the case of the domain described in “Abstract Entities,” if you specify a fetch request with the Graphic entity, the fetch returns matching instances of Circle, TextArea, and Line.

–  –  –

This document describes issues related to using and manipulating managed objects in your application.

Accessing and Modifying Properties Core Data automatically generates efficient public and primitive get and set accessor methods for modeled properties (attributes and relationships) of managed object classes (see “Managed Object Accessor Methods” (page 44)). When you access or modify properties of a managed object, you should use these methods directly.

Most relationships are inherently bidirectional. Any changes made to the relationships between objects should maintain the integrity of the object graph. Provided that you have correctly modeled a relationship in both directions and set the inverses, modifying one end of a relationship automatically updates the other end—see “Manipulating Relationships and Object Graph Integrity” (page 85).



Pages:     | 1 |   ...   | 7 | 8 || 10 | 11 |   ...   | 27 |


Similar works:

«Marc Torka Anke Borcherding Wissenschaftsunternehmer als Beruf? Berufsund professionssoziologische paper Überlegungen vor dem Hintergrund aktueller (Ent-)Differenzierungsphänomene der Wissenschaft Dieses Discussion Paper entstand im Rahmen des Projekts Wissenschaftsunternehmer: Typus, Merkmale und Erfolgsbedingungen von akademischen Grenzgängern, gefördert vom Bundesministerium für Bildung und Forschung (BMBF). discussion Discussion Paper SP III 2008-601 Mai 2008 Wissenschaftszentrum...»

«Universität zu Köln Humanwissenschaftliche Fakultät Department Heilpädagogik und Rehabilitation Lehrstuhl Sprachbehindertenpädagogik in schulischen und außerschulischen Bereichen Prof. Dr. Hans-Joachim Motsch Dipl.Log. Tanja Brüll Der „Wortschatzsammler“: Lexikalische Strategietherapie im Vorschulalter Eine randomisierte vergleichende Interventionsstudie (2009-2010) Das Ziel dieses Forschungsprojekts ist der Vergleich der Effektivität einer lexikalischen Strategietherapie mit der...»

«Konfirmation v on Tim Bertelmann Lukas Lauber Laurence Bieler Charlot Martens Leon Böcking Mats Menn Lisa Marie Daginnus Laura Müller Lea Drey Lennard Musch Michael Götz Niklas Schnell Alina Krasota Erika Stotz Ronja Laszlo Vanessa Vogt 4. Mai 2014 Erlöser-Kirche Siegen Musik zum Eingang (Klavier + Trompete) Eröffnung fröhlich – offen – locker – interessant – relaxed – was zu lachen, eher feierlich als festlich So habt Ihr beschrieben, wie Euer Konfirmations-Gottesdienst sein...»

«Journal for Baptist Theology and Ministry Vol. 2 No. 2 (Fall 2004): 135-153 John 3:13 and the Omnipresence of Jesus Christ R. Larry Overstreet, Ph.D. Director, Doctor of Ministry Program Professor of Pastoral Theology Northwest Baptist Seminary 4301 North Stevens Street Tacoma, WA 98407 Introduction The doctrine of the omnipresence of Christ, while He was alive on this earth, is an area of some disagreement among theologians. Some use texts such as Matthew 18:20 or 28:20 to acknowledge His...»

«EVEREST EXPEDITION TRIP NOTES Mount Everest 2016 Expedition Notes All material Copyright © Adventure Consultants Ltd 2015-16 During the spring season of 2016 Adventure Consultants will operate its twenty-third expedition to Mount Everest via the South Col from Nepal. This expedition will bring together the very best components in high altitude guiding to attempt Mt Everest. Calling on our many years of experience, our very qualified guides and Sherpa crew offer the most seasoned team in the...»

«Neural processes underlying conceptualization in language production Dissertation zur Erlangung des akademischen Grades doctor rerum naturalium (Dr. rer. nat.) genehmigt durch die Fakultät für Naturwissenschaften der Otto-von-Guericke-Universität Magdeburg von Dipl.-Psych. Boukje Habets geb. am 19-07-178 in Geleen Gutachter: Prof. Dr. Thomas Muente Prof. Dr. Tamara Swaab eingereicht am: 27.02.2007 verteidigt am: 31.05.2007 Acknowledgements Acknowledgements I would like to thank Prof. Dr....»

«The Baptist Catechism (Keach’s Catechism) Q. 1. Who is the first and best of beings? A. God is the first and best of beings. (Isaiah 44:6; Psalm 8:1; 97:9) Q. 2. What is the chief end of man? A. Man's chief end is to glorify God and to enjoy Him forever. (1 Cor. 10:31; Psalm 73:25-26) Q. 3. How do we know there is a God? A. The light of nature in man, and the works of God, plainly declare that there is a God; but His Word and Spirit only, do effectually reveal Him unto us for our salvation....»

«Normandy American Cemetery and Memorial THE AMERICAN BATTLE MONUMENTS COMMISSION AMERICAN BATTLE MONUMENTS COMMISSION Normandy American Cemitery and Memorial LOCATION The Normandy American Cemetery is situated on a cliff overlooking Omaha Beach and the English Channel just east of St-Laurent-sur-Mer and northwest of Bayeux in Colleville-sur-Mer. Travel time by train from the Gare St.-Lazare (St. Lazare railway station) in Paris to Bayeux is approximately 2 ½ hours. The rail service between...»

«Mustimuhw Information Solutions Inc. Data Governance Framework Data Governance Framework Framework and Associated Tools NAME OF NATION Distributed on: Month DD, YYYY Version: V1.0 Mustimuhw Information Solutions Inc. Data Governance Framework Mustimuhw Information Solutions Inc. Data Governance Framework Warranty Disclaimer This document and all of the information it contains are provided as is without warranty of any kind, whether express or implied. All implied warranties, including, without...»

«IT’S ALL IN THE GAME INHALT Benjamin Beil, Sascha Simons, Jürgen Sorg und Jochen Venus It’s all in the game! Jochen Venus Homecoming. Zur Simulation mentaler Karten in GTA San Andreas. 13 Andreas Rauscher Exile on Mean Street. Popkulturelles genre cruising in der Grand Theft Auto-Reihe Erik Fischer und Bettina Schlüter The music of Liberty City. Zur Konvergenz realer und virtueller Musikkulturen Rune Klevjer The Way of the Gun. Die Ästhetik des singleplayer first person shooters. 53...»





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