Exception thrown in NSOrderedSet generated accessors

后端 未结 25 2546
天涯浪人
天涯浪人 2020-11-22 09:07

On my Lion app, I have this data model:

\"enter

The relationship subitem

25条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 09:38

    I found using the method by LeeIII worked, but on profiling found it was drastically slow. It took 15 seconds to parse 1000 items. Commenting out the code to add the relationship turned 15 seconds into 2 seconds.

    My workaround (which is faster but much more ugly) involves creating a temporary mutable array then copying into the ordered set when all the parsing is done. (this is only a performance win if you are going to add many relationships).

    @property (nonatomic, retain) NSMutableArray* tempItems;
     ....
    @synthesize tempItems = _tempItems;
     ....
    
    - (void) addItemsObject:(KDItem *)value 
    {
        if (!_tempItems) {
            self.tempItems = [NSMutableArray arrayWithCapacity:500];
        }
        [_tempItems addObject:value];
    }
    
    // Call this when you have added all the relationships
    - (void) commitRelationships 
    {
        if (_tempItems) {
            self.items = [NSOrderedSet orderedSetWithArray:self.tempItems];
            self.tempItems = nil;
        }
    }
    

    I hope this help someone else!

提交回复
热议问题