Originally created by @matrosovDev on GitHub (Apr 30, 2019).
Hi could someone help or explain where I did mistake
I try to update workout.muscles Swift.Set which is a relationship One Workout To Many Muscles.
For the first call all works good, but all next calls just add only new inserted object to fetchExisting others seems are stored in some background context.
Originally created by @matrosovDev on GitHub (Apr 30, 2019).
Hi could someone help or explain where I did mistake
I try to update workout.muscles Swift.Set which is a relationship One Workout To Many Muscles.
For the first call all works good, but all next calls just add only new inserted object to fetchExisting others seems are stored in some background context.
Here is a link to question:
https://stackoverflow.com/questions/55921312/corestore-insert-array-as-relationship-objects-while-updating-data-model
adam
added the question label 2025-12-29 15:28:00 +01:00
Hi, this would be the general behavior of NSManagedObjects with regards to to-many properties.
First, as you have found out, the inverse relationship is important.
Second, Set is not recommended for unordered @NSManaged properties. Please use NSSet.
In that note, it should be treated as immutable. Which means you cannot insert objects directly to the NSSet value. There are two ways you can insert values.
@JohnEstropia commented on GitHub (May 1, 2019):
Hi, this would be the general behavior of `NSManagedObject`s with regards to to-many properties.
First, as you have found out, the inverse relationship is important.
Second, `Set` is not recommended for unordered `@NSManaged` properties. Please use `NSSet`.
In that note, it should be treated as immutable. Which means you cannot insert objects directly to the `NSSet` value. There are two ways you can insert values.
1. Assigning a new `NSSet` directly:
```swift
var muscles = (workout.muscles as! Set<MusclEntity>?) ?? []
muscles.insert(newMuscle)
workout.muscles = muscles as NSSet
```
2. Use KVO mutable accessors (creates an `NSMutableSet` proxy for you):
```swift
workout.mutableSetValueForKey(#keyPath(WorkoutEntity.muscles))
.addObject(newMuscle)
```
@matrosovDev commented on GitHub (May 2, 2019):
As John suggested we need to declare properties as NSSet instead Set and then we can use 2 code fragments above to make it done.
Answered here as well:
https://stackoverflow.com/questions/55921312/corestore-insert-array-as-relationship-objects-while-updating-data-model
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @matrosovDev on GitHub (Apr 30, 2019).
Hi could someone help or explain where I did mistake
I try to update workout.muscles Swift.Set which is a relationship One Workout To Many Muscles.
For the first call all works good, but all next calls just add only new inserted object to fetchExisting others seems are stored in some background context.
Here is a link to question:
https://stackoverflow.com/questions/55921312/corestore-insert-array-as-relationship-objects-while-updating-data-model
@JohnEstropia commented on GitHub (May 1, 2019):
Hi, this would be the general behavior of
NSManagedObjects with regards to to-many properties.First, as you have found out, the inverse relationship is important.
Second,
Setis not recommended for unordered@NSManagedproperties. Please useNSSet.In that note, it should be treated as immutable. Which means you cannot insert objects directly to the
NSSetvalue. There are two ways you can insert values.NSSetdirectly:NSMutableSetproxy for you):@matrosovDev commented on GitHub (May 2, 2019):
As John suggested we need to declare properties as NSSet instead Set and then we can use 2 code fragments above to make it done.
Answered here as well:
https://stackoverflow.com/questions/55921312/corestore-insert-array-as-relationship-objects-while-updating-data-model