title: How to add element to NSOrderedSet for Core Data?
body: <p>I did some studies on this <a href="https://stackoverflow.com/questions/7385439/exception-thrown-in-nsorderedset-generated-accessors/7922993#7922993">post</a>. But none of its solution work for me. Let me explain what I did:</p>
  
  <p><img src="https://i.stack.imgur.com/4q4ui.jpg" alt="enter image description here"></p>
  
  <p>It's actually very similar with the mentioned post.</p>
  
  <p>Category.h</p>
  
  <pre><code>@class Category, Item;
  
  @interface Category : NSManagedObject
  
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) NSOrderedSet *items;
  @property (nonatomic, retain) Category *parentcategory;
  @property (nonatomic, retain) NSSet *subcategories;
  
  @end
  
  @interface Category (CoreDataGeneratedAccessors)
  
  - (void)insertObject:(Item *)value inItemsAtIndex:(NSUInteger)idx;
  - (void)removeObjectFromItemsAtIndex:(NSUInteger)idx;
  - (void)insertItems:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
  - (void)removeItemsAtIndexes:(NSIndexSet *)indexes;
  - (void)replaceObjectInItemsAtIndex:(NSUInteger)idx withObject:(Item *)value;
  - (void)replaceItemsAtIndexes:(NSIndexSet *)indexes withItems:(NSArray *)values;
  - (void)addItemsObject:(Item *)value;
  - (void)removeItemsObject:(Item *)value;
  - (void)addItems:(NSOrderedSet *)values;
  - (void)removeItems:(NSOrderedSet *)values;
  - (void)addSubcategoriesObject:(Category *)value;
  - (void)removeSubcategoriesObject:(Category *)value;
  - (void)addSubcategories:(NSSet *)values;
  - (void)removeSubcategories:(NSSet *)values;
  
  @end
  </code></pre>
  
  <p>In my view controller, I load one of the category:</p>
  
  <pre><code>NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kEntityCategory];
  request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor
                                                      sortDescriptorWithKey:kAttributeCategoryName ascending:YES
                                                      selector:@selector(localizedCaseInsensitiveCompare:)]];
  
  NSError *error = nil;
  AppDelegate* appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  
  NSArray* data = [appDelegate.dataCenter.document.managedObjectContext executeFetchRequest:request error:&amp;error];
  if ( data.count &gt; 0)
  {
      self.category = [data objectAtIndex:0];
  }
  </code></pre>
  
  <p>I have a button in navigation bar for testing purpose. When it's clicked I'll copy the first item and add to the end.</p>
  
  <pre><code>Item* item = [self.category.items objectAtIndex:0];
  
  [self.category willChangeValueForKey:@"items"];
  NSMutableOrderedSet *tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.category.items];
  [tempSet addObject: item];
  self.category.items = tempSet;
  [self.category didChangeValueForKey:@"items"];
  </code></pre>
  
  <p>The problem is the item is not added to database at all. (I did some testing, for example, I modify the item content. It proves that the database has no problem at all.) But I don't know the reason why the item is not to the database. Can anybody help? Thanks</p>