13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 8: Data Management with Core Data 245As a result, it’s important to remember that managed objects are tied to a single context, andthus if you need to store them in an instance somewhere, you should likely instead store theirobject ID, represented by the NSManagedObjectID class and obtained by sending -objectID to amanaged object.You can reverse the process by handing your NSManagedObjectID into NSManagedObjectContext’sobjectWithID: or existingObjectWithID:error: methods. The difference between the two isimportant: the first method may return a fault, meaning that the data isn’t guaranteed to existwhen the time comes to access it (trust me, I’ve run into this situation in numerous places). Thesecond, however, will always return a fully faulted object or nil. Thus if the current thread’scontext hasn’t yet learned of a change on-disk that removed this object, it will discover thiswhen it attempts to load its data. If you fetch a fault referring to a deleted object, any attempt toaccess its values will result in Core Data throwing an exception.Listing 8-10 shows some useful utility code I’ve used in a few shipping applications to help withthread confinement. It provides a means to fetch (or create, if necessary) a managed objectcontext specific to the calling thread.Listing 8-10. Per-Thread NSManagedObjectContextsstatic NSString * const AQPerThreadManagedObjectContext =@"AQPerThreadManagedObjectContext";void StoreManagedObjectContextForCurrentThread(NSManagedObjectContext * context){[[[NSThread currentThread] threadDictionary] setObject: contextforKey: AQPerThreadManagedObjectContext];}NSManagedObjectContext * PerThreadManagedObjectContext(void){NSManagedObjectContext * result = [[[NSThread currentThread] threadDictionary]objectForKey: AQPerThreadManagedObjectContext];if ( result ! = nil )return ( result );}NSManagedObjectContext * moc = [[NSManagedObjectContext alloc] init];[moc setMergePolicy: NSMergeByPropertyObjectTrumpMergePolicy];[moc setPersistentStoreCoordinator: GetPersistentStoreCoordinator()];StoreManagedObjectContextForCurrentThread(moc);[moc release]; // now owned by the thread dictionaryreturn ( moc );To use this, you first create your regular managed object context during applicationstartup on the main thread. This is important: instances of NSManagedObjectContextbehave differently if created on the main thread. Having created it, pass it toStoreManagedObjectContextForCurrentThread(). Once this is done, simply callPerThreadManagedObjectContext() any time you need to access your data store.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!