ios - Is copying a collection before iteration enough to prevent synchronization problems? -


I have a session property, a temporary set I need to iterate over the collection, But simultaneously, I can change the archive in another method:

  - session (session) withID: (NSString *) sessionID {for (session * candidates _sessions) {/ * some * Return zero; } - (zero) doSomethingElse {[_sessions removed object: ...]; }  

This thread is not secure-a bullet-proof version @ synchronized or _sessions to sort access to a dispatch Will use the queue. But how appropriate is it to copy the set before it?

  - (session *) session with ID: (NSString *) for session ID {copy * candidates * in session *) * something * /} return zero; }  

I do not care too much about the performance difference.

But how appropriate is it to copy it before going to set?

As presented, this thread is not guaranteed to be secure. You will need to guarantee that the _sessions to -copy Has not been mutated during. Then it is safe to operate on an irreversible copy, and the mutation of the _sessions may be on a secondary thread or in your implementation.

In many cases with coca collections, you will find that by declaring the property as the copy type NSSet , the irreversible ivars and on the set Copy is better to use . In this way, you make a copy of the write / set, and then avoid reading the copy. Based on the fact your program is actually implemented, it has the ability to reduce copies. Generally, this is not enough alone, and you will need some high level of synchronization.

Also remember that the session s thread in the set can not be secure even after accessing your archive is protected properly, Access to objects may need to be protected.


Comments