Fast enumeration using for (object in collection)
has nothing to do with threads. All of this code runs on the same thread.
Is it safe to perform save operations on an NSManagedContext object when using Objective-C fast enumeration
-
01-07-2022 - |
Question
I was wondering if this sort of code would be ok to use
NSManagedObjectContext *moc = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
for (NSDictionary *dict in objects) {
Object *object = [NSEntityDescription insertNewObjectForEntityForName:@"object"
inManagedObjectContext:moc];
// Populate model fields with appropriate dictionary keys
}
Reason why I ask is that you're advised to not pass references to a managed object context object across threads and wondering if that is indeed what is occurring here.
Solution
OTHER TIPS
If that insert operation is threaded you should consider creating a backgrounded NSManagedObjectContext to handle it, which is a post way too big and answered on SO before.
You are using one ManagedObjectContext,the are no thread problem in your code. You are doing it the right way.All the code is sequential. If you want to use multiple context, each one in a thread, you should take care of multithreading.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow