How to efficiently get all instances from deeper level in Cocoa model?
        Posted  
        
            by Johan Kool
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Johan Kool
        
        
        
        Published on 2010-03-19T09:05:14Z
        Indexed on 
            2010/03/19
            9:11 UTC
        
        
        Read the original article
        Hit count: 205
        
In my Cocoa Mac app I have an instance A which contains an unordered set of instances B which in turn has an ordered set of instances C. An instance of C can only be in one instance B and B only in one A.
I would like to have an unordered set of all instances C available on instance A. I could enumerate over all instances B each time, but that seems expensive for something I need to do often. However, I am a bit worried that keeping track of instances C in A could become cumbersome and be the cause of inconsistencies, for example if an instance C gets removed from B but not from A.
Solution 1 Use a NSMutableSet in A and add or remove C instances whenever I do the same operation in B.
Solution 2 Use a weak referenced NSHashTable in A. When deleting a C from B, it should disappear for A as well.
Solution 3 Use key value observing in A to keep track of changes in B, and update a NSMutableSet in A accordingly.
Solution 4 Simply iterate over all instances B to create the set whenever I need it.
Which way is best? Are there any other approaches that I missed?
NB I don't and won't use CoreData for this app.
© Stack Overflow or respective owner