keerushek / Survey

Nimbl3 Assignment
0 stars 0 forks source link

PerformBlock/ PerformBlockAndWait #13

Open theamorn opened 7 years ago

theamorn commented 7 years ago

Hi, This is Amorn from Nimbl3

When saving context in CoreData, Apple recommend you to use either one to make sure your data is safe to access

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/Concurrency.html

keerushek commented 7 years ago

Hi Amorn,

All the Coredata Handling classes in the NSOperation classes (CommandAccessTokenRefresh & CommandFetchSurveyList) make use of privateContext which points to the code in CommonManagedObject

+ (NSManagedObjectContext *)privateContext { NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [context setParentContext:[self context]]; return context; }

For the classes accessing Coredata in the main Thread i made use of MainContext. As the fetch and write will be quicker from the main Thread.

Once the project gets bigger and complex, then yeah there might be race conditions for writing into persistent coordinator. But using NSOperations and better business logic that can be avoided. Reference link: http://stackoverflow.com/questions/27260578/multiple-nsmanagedobjectcontexts-preventing-race-conditions-and-deadlocks

theamorn commented 7 years ago

Ok cool, I will update my knowledge. Thanks for link