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

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
source share