Updating existing set of relationships issue #270

Closed
opened 2025-12-29 15:28:00 +01:00 by adam · 2 comments
Owner

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

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
adam closed this issue 2025-12-29 15:28:00 +01:00
Author
Owner

@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, 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:
var muscles = (workout.muscles as! Set<MusclEntity>?) ?? []
muscles.insert(newMuscle)
workout.muscles = muscles as NSSet
  1. Use KVO mutable accessors (creates an NSMutableSet proxy for you):
workout.mutableSetValueForKey(#keyPath(WorkoutEntity.muscles)) 
    .addObject(newMuscle)
@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) ```
Author
Owner

@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

@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
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#270