0

I am trying to perform a “deep copy” of managed objects from a source persistent store to an existing persistent store. Borrowing code for the Task to establish To-Many Relationship in Target-Context. The complete code is available Here

My confusion is in this below method

- (void)establishToManyRelationship:(NSString*)relationshipName
                         fromObject:(NSManagedObject*)object // “Copied Object" in TargetContext
                      withSourceSet:(NSMutableSet*)sourceSet // NSMutableSet in Source Context.
{
    if (!object || !sourceSet || !relationshipName) {
        NSLog(@"SKIPPED establishing a To-Many relationship from %@", [self objectInfo:object]);
        NSLog(@"Due to missing Info!");
        return;
    }

    //  Returns a mutable set proxy that provides read-write access to the unordered to-many relationship specified by a given key.
    NSMutableSet *copiedSet =[object mutableSetValueForKey:relationshipName];

    //in sourceContext, "Many"side of relationship represented by a Set, where "relatedObject" belongs to
    for (NSManagedObject *relatedObject in sourceSet) {

        // Related Copied Object in TargetContext
        NSManagedObject *copiedRelatedObject =
            [self copyUniqueObject:relatedObject toContext:object.managedObjectContext];

        if (copiedRelatedObject) {
            [copiedSet addObject:copiedRelatedObject];
            NSLog(@"A copy of %@ is now related via To-Many '%@' relationship to %@", [self objectInfo:object],
                  relationshipName,
                  [self objectInfo:copiedRelatedObject]);
        }
    }
    // REMOVE the relationship from memory after it is committed to disk
    [CoreDataImporter saveContext:object.managedObjectContext];

    [object.managedObjectContext refreshObject:object mergeChanges:NO];
}

My questions is, after

[copiedSet addObject:copiedRelatedObject];

, which is performed on a "collection proxy object" (produced by mutableSetValueForKey: invocation), and has NO result returned (explicitly either by parameters or return-value) from this method

- (void)establishToManyRelationship:(NSString*)relationshipName
                         fromObject:(NSManagedObject*)object 
                      withSourceSet:(NSMutableSet*)sourceSet

, how can I ensure there is a NSMutableSet (composed by Copied-Related-Objects) created in Target Context? Or I never need to have a reference for such "Copied-Set" in Target Context? Could Object Graph in Target Context be just established magically around "Copied Object" (representing the ONE-side of "To-Many" relationship) by using "collection proxy object" implemented via KVC?

hyde
  • 60,639
  • 21
  • 115
  • 176
J-Q
  • 374
  • 5
  • 15

1 Answers1

0

After a few testings, I figured it out.

The 2nd parameter "(NSManagedObject*)object" can be manipulated via the proxy (NSMutableSet *copiedSet), which is returned by KVC invocation on "mutableSetValueForKey:".

Any operations loaded on the proxy, will magically/actually reflect back on the relationships of "(NSManagedObject*)object" (as this MO's properties).

J-Q
  • 374
  • 5
  • 15