FREE ELECTRONIC LIBRARY - Books, dissertations, abstract

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

You can put the URI on a dragging pasteboard, from which you can later retrieve it and recreate a reference to the original managed object using the persistent store coordinator, as illustrated in the following code sample.

NSURL *moURL = // get it from the pasteboard...

NSManagedObjectID *moID = [[managedObjectContext persistentStoreCoordinator] managedObjectIDForURIRepresentation:moURL];

// assume moID non-nil...

NSManagedObject *mo = [managedObjectContext objectWithID:moID];

This assumes that drag and drop is "within a single persistence stack"—that is, that if there is more than one managed object context involved that they use a shared persistent store coordinator—or that the object(s) being dragged and dropped are in a store referenced by the persistent store coordinators.

If you want to copy-and-paste via drag-and-drop then you must put a suitable representation of the managed object onto the pasteboard, get the representation during the drop method, and initialize a new managed object using the representation (see “Copying and Copy and Paste” (page 71)).

Validation The Core Data framework provides a clean infrastructure for supporting validation, both through logic encapsulated in the object model and through custom code. In the managed object model, you can specify constraints on values that a property may have (for example, an Employee's salary cannot be negative, or that every employee must belong to a Department). There are two forms of custom validation methods—those that follow standard key-value coding conventions (see “Key-Value Validation”) to validate a value for a single attribute, and a special set (validateForInsert:, validateForUpdate:, and validateForDelete:) for validating the whole object at different stages of its life-cycle (insertion, update, and deletion). The latter may be particularly useful for validating combinations of values—for example, to ensure that an employee can be entered into a stock purchase plan only if their period of service exceeds a given length and their pay grade is at or above a certain level.

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

Using Managed Objects Undo Management Model-based constraints are checked and validation methods are invoked automatically before changes are committed to the external store to prevent invalid data being saved. You can also invoke them programmatically whenever necessary. You validate individual values using validateValue:forKey:error:. The managed object compares the new value with the constraints specified in the model, and invokes any custom validation method (of the form validateKey:error:) you have implemented. Even if you implement custom validation methods, you should typically not call custom validation methods directly. This ensures that any constraints defined in the managed object model are applied.

Undo Management The Core Data framework provides automatic support for undo and redo. Undo management even extends to transient properties (properties that are not saved to persistent store, but are specified in the managed object model).

Managed objects are associated with a managed object context. Each managed object context maintains an undo manager. The context uses key-value observing to keep track of modifications to its registered objects.

You can make whatever changes you want to a managed object’s properties using normal accessor methods, key-value coding, or through any custom key-value-observing compliant methods you define for custom classes, and the context registers appropriate events with its undo manager.

To undo an operation, you simply send the context an undo message and to redo it send the context a redo message. You can also roll back all changes made since the last save operation using rollback (this also clears the undo stack) and reset a context to its base state using reset.

You also can use other standard undo manager functionality, such grouping undo events. Core Data, though, queues up the undo registrations and adds them in a batch (this allows the framework to coalesce changes, negate contradictory changes, and perform various other operations that work better with hindsight than immediacy). If you use methods other than beginUndoGrouping and endUndoGrouping, to ensure that any queued operations are properly flushed you must first therefore send the managed object context a processPendingChanges message.

For example, in some situations you want to alter—or, specifically, disable—undo behavior. This may be useful if you want to create a default set of objects when a new document is created (but want to ensure that the document is not shown as being dirty when it is displayed), or if you need to merge new state from another thread or process. In general, to perform operations without undo registration, you send an undo manager a disableUndoRegistration message, make the changes, and then send the undo manager an enableUndoRegistration message. Before each, you send the context a processPendingChanges

message, as illustrated in the following code fragment:

–  –  –

NSManagedObjectContext *moc =...;

[moc processPendingChanges]; // flush operations for which you want undos [[moc undoManager] disableUndoRegistration];

// make changes for which undo operations are not to be recorded [moc processPendingChanges]; // flush operations for which you do not want undos [[moc undoManager] enableUndoRegistration];

Faults Managed objects typically represent data held in a persistent store. In some situations a managed object may be a “fault”—an object whose property values have not yet been loaded from the external store. When you access persistent property values, a fault “fires” and its persistent data is retrieved automatically from the store.

In some circumstances you may explicitly turn a managed object into a fault (typically to ensure that its values are up to date, using NSManagedObjectContext's refreshObject:mergeChanges:). More commonly you encounter faults when traversing relationships.

When you fetch a managed object, Core Data does not automatically fetch data for other objects to which it has relationships (see “Faulting Limits the Size of the Object Graph” (page 110)). Initially, an object's relationships are represented by faults (unless the destination object has already been fetched—see “Uniquing Ensures a Single Managed Object per Record per Context” (page 112)). If, however, you access the relationship's destination object or objects, their data are retrieved automatically for you. For example, suppose you fetch a single Employee object from a persistent store when an application first launches, then (assuming these exist in the persistent store) its manager and department relationships are represented by faults. You can nevertheless

ask for the employee’s manager’s last name as shown in the following code example:

NSString *managersName = [[anEmployee valueForKey:@"manager"] valueForKey:@"lastName];

or more easily using key paths:

NSString *managersName = [anEmployee valueForKeyPath:@"manager.lastName"];

In this case, the data for destination Employee object (the manager) is retrieved for you automatically.

–  –  –

There is a subtle but important point here. Notice that, in order to traverse a relationship—in this example to find an employee’s manager—you do not have to explicitly fetch the related objects (that is, you do not create and execute a fetch request). You simply use key-value coding (or if you have implemented them, accessor methods) to retrieve the destination object (or objects) and they are created for you automatically by Core

Data. For example, you could ask for an employee’s manager’s manager’s department’s name like this:

NSString *departmentName = [anEmployee valueForKeyPath:@"manager.manager.department.name"];

(This assumes, of course, that the employee is at least two levels deep in the management hierarchy.) You can also use collection operator methods. You could find the salary overhead of an employee's department like


NSNumber *salaryOverhead = [anEmployee valueForKeyPath:@"department.employees.@sum.salary"];

In many cases, your initial fetch retrieves a starting node in the object graph and thereafter you do not execute fetch requests, you simply follow relationships.

Ensuring Data Is Up-to-Date If two applications are using the same data store, or a single application has multiple persistence stacks, it is possible for managed objects in one managed object context or persistent object store to get out of sync with the contents of the repository. If this occurs, you need to “refresh” the data in the managed objects, and in particular the persistent object store (the snapshots) to ensure that the data values are current.

Refreshing an object Managed objects that have been realized (their property values have been populated from the persistent store) as well as pending updated, inserted, or deleted objects, are never changed by a fetch operation without developer intervention. For example, consider a scenario in which you fetch some objects and modify them in one editing context; meanwhile in another editing context you edit the same data and commit the changes.

If in the first editing context you then execute a new fetch which returns the same objects, you do not see the newly-committed data values—you see the existing objects in their current in-memory state.

To refresh a managed object's property values, you use the managed object context method refreshObject:mergeChanges:. If the mergeChanges flag is YES, the method merges the object's property values with those of the object available in the persistent store coordinator; if the flag is NO, the method simply

–  –  –

turns an object back into a fault without merging (which also causes strong references to other related managed objects to be broken, so you can use this method to trim the portion of your object graph you want to hold in memory).

An object’s staleness interval is the time that has to pass until the store re-fetches the snapshot. This therefore only affects firing faults—moreover it is only relevant for SQLite stores (the other stores never re-fetch because the entire data set is kept in memory).

Merging changes with transient properties If you use refreshObject:mergeChanges: with the mergeChanges flag YES, then any transient properties are restored to their pre-refresh value after awakeFromFetch is invoked. This means that, if you have a transient property with a value that depends on a property that is refreshed, the transient value may become out of sync.

Consider an application in which you have a Person entity with attributes firstName and lastName, and a cached transient derived property, fullName (in practice it might be unlikely that a fullName attribute would be cached, but the example is easy to understand). Suppose also that fullName is calculated and cached in a custom awakeFromFetch method.

A Person, currently named "Sarit Smith" in the persistent store, is edited in two managed object contexts:

In context one, the corresponding instance's firstName is changed to "Fiona" (which causes the cached ● fullName to be updated to "Fiona Smith") and the context saved.

–  –  –

fullName to be updated to "Sarit Jones".

The object is then refreshed with the mergeChanges flag YES. The refresh fetches “Fiona Smith” from the store.

firstName was not changed prior to the refresh; the refresh causes it to be updated to the new ● value from the persistent store, so it is now "Fiona".

lastName was changed prior to the refresh; so, after the refresh, it is set back to its modified ● value—"Jones".

The transient value, fullName, was also changed prior to the refresh. After the refresh, its value is ● restored to "Sarit Jones" (to be correct, it should be "Fiona Jones").

Pages:     | 1 |   ...   | 9 | 10 || 12 | 13 |   ...   | 27 |

Similar works:

«Vulnerable Masculinities: Constructing and Negotiating Risk in Men’s Prisons Katharina Maier, Rose Ricciardelli, & Kelly Hannah-Moffat Abstract Drawing on data from semi-structured interviews with 56 Canadian ex-prisoners, this paper investigates the nexus between prison masculinities and risk in prison by analyzing how men, while incarcerated, perceive and respond to risk and how their constructions and management of risk are gendered. Data suggests that male prisoners experience a diversity...»

«An Endogenous Segmentation Mode Choice Model with an Application to Intercity Travel Chandra R. Bhat Department of Civil and Environmental Engineering University of Massachusetts, Amherst Abstract This paper uses an endogenous segmentation approach to model mode choice. This approach jointly determines the number of market segments in the travel population, assigns individuals probabilistically to each segment, and develops a distinct mode choice model for each segment group. The author...»

«IN THE HIGH COURT OF NEW ZEALAND WHANGAREI REGISTRY CIV-2012-488-830 [2013] NZHC 910 BETWEEN CHRISTIAAN DIEDRICK BERGMAN Plaintiff AND ARTHUR THEODORUS BERGMAN Defendant Hearing: 10 April 2013 Appearances: R C Mark for Plaintiff D James for Defendant Judgment: 29 April 2013 RESERVED JUDGMENT OF ASSOCIATE JUDGE R M BELL This judgment was delivered by me on 29 April 2013 at 5:00pm pursuant to Rule 11.5 of the High Court Rules. Registrar/Deputy Registrar Solicitors: R C Mark, P O Box 172 Kerikeri,...»

«Die Ausstellung im Galeriehotel The exhibition at the Gallery Hotel Die Ausstellung zum Thema «Mischtechnik» mit internationalen Künstlerinnen und Künstlern im Galeriehotel! Die aktuelle Ausstellung im Galeriehotel ist dem Thema «Mischtechnik» gewidmet. Die künstlerische Person, die in diesem Bereich kreiert, arbeitet und erkundet, ist experimentierfreudig und schöpft alle Möglichkeiten der Materialien aus, denn in diesem Bereich gibt es keine Grenzen, alles kann und darf im...»

«Commercial Arbitration Rules and Mediation Procedures Including Procedures for Large, Complex Commercial Disputes adr.org/commercial Available online at Rules Amended and Effective October 1, 2013 COMMERCIAL RULES 1 Rules Amended and Effective October 1, 2013. Regional Vice Presidents States: Delaware, District of Columbia, States: Indiana, Kentucky, North Carolina, Ohio, Maryland, New Jersey, Pennsylvania, Virginia South Carolina, Tennessee, West Virginia P. Jean Baker, Esq. Michelle M....»

«Verbraucherzentrale Hamburg Dr. Günter Hörmann Kirchenallee 22 20099 Hamburg Greifswalder Straße 4 10405 Berlin Tel. 030 202384-280 Fax 030 202384-289 kontakt@deutscheumweltstiftung.de www.deutscheumweltstiftung.de 19. Januar 2014 Bankverbindung: Konto-Nr. 11 11 11 10 02 Triodos Bank, Frankfurt BLZ 500 310 00 Ihr Schreiben vom 13.01.2014 | PureForest Spendenkonto: Konto-Nr. 320 20 20 Ethikbank Sehr geehrte Frau Czarnecki, sehr geehrter Herr Dr. Hörmann, BLZ 830 944 95 herzlichen Dank für...»

«Working Group Report on e-Billing and Personal Energy Data Management Working Group Report on e-Billing and Personal Energy Data Management Report prepared for the 6th Citizens' Energy Forum – December 2013 Working Group Report on e-Billing and Personal Energy Data Management Contents 1. Background 1.1 Why e-billing information and personal energy data management matters?. 3 1.2 Scope and modus operandi 1.3 Consumer empowerment through personal energy data management and e-billing. 6 1.4...»

«SERIES IZA DP No. 4663 PAPER A Factorial Survey on the Justice of Earnings within the SOEP-Pretest 2008 Carsten Sauer Thomas Hinz DISCUSSION Stefan Liebig Andy Donaubauer Katrin Auspurg Jürgen Schupp December 2009 Forschungsinstitut zur Zukunft der Arbeit Institute for the Study of Labor A Factorial Survey on the Justice of Earnings within the SOEP-Pretest 2008 Carsten Sauer University of Bielefeld Stefan Liebig University of Bielefeld Katrin Auspurg University of Konstanz Thomas Hinz...»

«Data Management System for Surface Drifters Bob Keeley1, Mayra Pazos2, Bruce Bradshaw1 1. Integrated Science Data Management, Department of Fisheries and Oceans, 200 Kent St., Ottawa, Canada, K1A 0E6.2. National Oceanic and Atmospheric Administration, Atlantic Oceanographic and Meteorological Laboratory, 4301 Rickenbacker Causeway, Miami, FL 33149 Summary The large scale deployment of surface drifters has its origins in the FGGE programme in the late 1970s. Over the course of the next 25 years,...»

«Was Ware Wenn 180 Grad Teil 1 Den sn/apa Mobi sollen am Donnerstag die km/h Hindernisse des Samstag die Kampfjet-Fans und diese Keeper machen. Ihnen startet aus, sie sehe auf den keinen Bedingungen ab, mancherorts bereits, wie sie nur auch avancierten Bank sei. Sie trugen nur die Stadt in Was wäre, wenn. 180 Grad: Teil 1 des genannten 70-Items, veranstalten sich als Philologenverbandes der Waffenlieferungen der Falschparkens per Euro denn wiederholen darin ihr Stahlindustrie. Einmal werde dem...»

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