FREE ELECTRONIC LIBRARY - Books, dissertations, abstract

Pages:     | 1 ||

«Core Data in Objective-C, Third Edition Data Storage and Management for iOS and OS X This PDF file contains pages extracted from Core Data in ...»

-- [ Page 2 ] --

The call to -executeRequest: error: is a blocking call. This means that the call can take a significant amount of time. Still far less than loading all of the objects into memory, performing the change and saving them back out to disk, but it will still take time. This is another argument for using the API against the private writer context instead of the context that the user interface is associated with.

Notifying the Application of Changes Once we have made changes on disk we need to notify the contexts of these changes.

In this particular example, it is not strictly necessary. Most likely the User Interface has not touched any of these objects yet. If the objects have not been loaded into memory then there is no risk of a conflict. However, it is best that we do not assume they have not been loaded yet. Users can be very clever.

There are two basic ways to notify our NSManagedObjectContext instances of the changes. We can reset each object individually in each NSManagedObjectContext that it might be associated with, or we can use the new API that was added in iOS 9.0. Lets look at the harder way first.

Manual Object Refreshing If the situation calls for it, we can instruct each instance of NSManagedObject to refresh individually. This might make sense if we have a view that is observing one specific instance of an entity or we have a user interface that is watching a small subset of objects.


- (void)manuallyRefreshObjects:(NSArray*)objectIDArray;

{ NSManagedObjectContext *moc = [self managedObjectContext];

[moc performBlockAndWait:^{ [objectIDArray enumerateObjectsUsingBlock:^(NSManagedObjectID *objectID, NSUInteger index, BOOL *stop) { NSManagedObject *object = [moc objectRegisteredForID:objectID];

if (!object || [object isFault]) return;

[moc refreshObject:object mergeChanges:YES];




• Click HERE to purchase this book now. discuss •8 The -manuallyRefreshObjects: method accepts an NSArray of NSManagedObjectID instances and walks through that array. Since this method is going to be working with the NSManagedObjectContext that is on the main queue, we want a guarantee that our code will be also executed on the main queue. Therefore we start by executing the code in a -performBlock: to insure we are on the correct queue.

Inside of the block we iterate over the array of NSManagedObjectID instances and retrieve a reference to the associated NSManagedObject. Note that we are using

-objectRegisteredForID: in this method. -objectRegisteredForID: will only return a nonnil result if the object is registered with the referenced NSManagedObjectContext.

If it is not referenced we certainly don’t want to load it so this method is a perfect fit. From that call we need to see if we got an object back and that it is not a fault. If it is a fault then we don’t need to refresh it as the values are not in memory.

Once we confirm the NSManagedObject is registered and is not a fault we then call -refreshObject: mergeChanges: which will force the object to reload from the persistent store.

That’s a fair amount of work for each individual NSManagedObject in the array.

Fortunately there is an easier way.

Remote Notifications As part of the update for iOS 9.0 and OS X 10.11, the Core Data team gave us a new class method to handle this situation. We can now call +[NSManagedObjectContext mergeChangesFromRemoteContextSave: intoContexts:] to handle changes that occurred outside of our Core Data stack.

The method accepts a dictionary of arrays of NSManagedObjectID instances (either true NSManagedObjectID objects or NSURL representations of them) and also accepts an array of NSManagedObjectContext instances.

Like traditional NSManagedObjectContextDidSaveNotification calls, we can pass in objects that are in three possible states: inserted, updated and deleted.


- (void)mergeExternalChanges:(NSArray*)objectIDArray ofType:(NSString*)type { NSDictionary *save = @{type : objectIDArray};

NSArray *contexts = @[[self managedObjectContext], [self writerContext]];

–  –  –

Note in this method that we are not concerned with what queue things are being executed on. The API call handles that for us. Also note that we are able to update all of the contexts that exist in our application at once.

This method will basically do the same thing that we did in our manual method. But it will handle it for every context we give it and it is faster than our manual method. This is the recommended way to consume remote notifications. With this method our original bulk update call can easily notify the rest of the application of the changes.

–  –  –

Pages:     | 1 ||

Similar works:

«Clogged Conduits: A Defendant’s Right to Confront His Translated Statements Casen B. Ross† A word is not a crystal, transparent and unchanged, it is the skin of a living thought and may vary greatly in color and content according to the circumstances and the time in which it is used. Justice Oliver Wendell Holmes1 INTRODUCTION Recall the childhood game “telephone.” Children are arranged in a large circle, and one child whispers a short phrase to the child on his or her left. That child...»

«American ascendancy: Conceptualizing contemporary hegemony Mark Beeson University of Queensland Abstract: This paper provides a theoretical framework within which to understand ‘American hegemony’. It reviews the major realist, liberal, Marxist, and constructivist understandings of American power, before arguing that only an eclectic approach to theorizing can capture the multiple aspects of the American ascendancy. Paper for symposium on Bush and Asia: America’s Evolving Relations with...»

«Internal Oversight Service Evaluation Section IOS/EVS/PI/111 REV. Original: English Evaluation of the Abdus Salam International Centre for Theoretical Physics September 2011 Authors: John Clark, Technopolis Carlos Hinojosa, Technopolis With contributions from: Jos Vaessen (task manager), UNESCO-IOS Clinton Watson, UNESCO-IOS Table of Contents Executive Summary 1 Background 1 Objective 1 Methodology 1 Overall conclusion 2 Key findings and recommendations 2 1. Introduction 5 1.1 Context of the...»

«Arbeitspapier / Abteilung BWL Sabine Wesely Führungserfolg durch Führungspersönlichkeit und Führungsverhalten Arbeitspapier 02-2014 ISSN Nr. 1436-1035 (print) ISSN Nr. 1436-1507 (Internet) Sabine Wesely: Führungserfolg durch Führungspersönlichkeit und Führungsverhalten 1 Führungserfolg durch Führungspersönlichkeit und Führungsverhalten Inhaltsverzeichnis Seite 1. Einführung.. 1 2. Persönlichkeit, Eigenschaften und Verhalten..2 3. Führung..11 4. Wesensgemäße Führung:...»

«FIW Studien – FIW Research Reports FIW Research Report N° 016 / Foreign Direct Investment June 2008 The Impact of Outward FDI in Central and Eastern Europe on Employment in the EU-15 Countries Falk, M., Wolfmayr, Y. Abstract This paper has examined the home market effects of outward FDI into the CEE region based on the AMADEUS firm-level database. In particular, we investigated the substitution possibilities between foreign affiliate employment and parent company employment by distinguishing...»

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