How to add an element to NSOrderedSet for master data?

I have done some research on this post. But none of his solutions work for me. Let me explain what I did:

enter image description here

Actually it is very similar to the mentioned post.

Category.h

@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 

In my opinion, the controller loads one of the categories:

 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:&error]; if ( data.count > 0) { self.category = [data objectAtIndex:0]; } 

I have a button in the navigation bar for testing. When he clicks, I will copy the first element and add it to the end.

 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"]; 

The problem is that the item is not added to the database at all. (I did some tests, for example, I modify the contents of an element. This proves that there are no problems in the database at all.) But I do not know why the element is not related to the database. Can anyone help? Thanks

+4
source share
3 answers

The problem is resolved. There are two problems:

  • Unable to insert / add existing item
  • I have a stupid name conflict. Therefore, the suggestion of this post is good.

Thanks.

+2
source

Swift:

 let mutableItems = items.mutableCopy() as! NSMutableOrderedSet mutableItems.addObject(anItem) items = mutableItems.copy() as! NSOrderedSet 

Objective-C:

 NSMutableOrderedSet *mutableItems = (NSMutableOrderedSet *)items.mutableCopy; [mutableItems addObject:anItem]; items = (NSOrderedSet *)mutableItems.copy; 
+16
source

Another way to do this is by using the mutableOrderedSetValueForKey function in NSKeyValueCoding. Could you say

owningObject.mutableOrderedSetValueForKey("items").addObject(anItem)

Unfortunately, this does not give you a time check for "time".

0
source

Source: https://habr.com/ru/post/1432695/


All Articles