문제

I need to update a value for each arranged object in Core Data, and I am currently using a loop. But I want to know if the following code is thread-safe or not, and why.

ManagedObjectContext is not thread safe, explained here

[myArrayController.arrangedObjects enumerateObjectsWithOptions:NSEnumerationConcurrent 
                                                    usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    // do some work

    [obj setValue:@"some value" forKey:@"aKey"];            
}]
도움이 되었습니까?

해결책

It's safe in the sense that, while multiple copies of the block may run concurrently, they're all working on different objects. Assuming that your call to setValue:forKey: doesn't have side effects on other objects in the collection, of course.

It's not safe in the sense that if another thread or queue attempts to access the objects in this collection, there's nothing to stop it from messing with the same object at the same time. The block doesn't lock out other actions in other places, so if you might be using these objects somewhere else at the same time, you'll need more precautions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top