0

I am working with coreData one -to many relationship. For example folder - file. So what I want to do is that take a file from one folder and copy it to another folder.

So for example 
folder   A  B
file     a  b
file     c  d

Now I want to copy file c from folder A to folder B and it should look this way

folder A B
file   a b
file   c d
file     c

To perform this operation I have written this code This is happening in some View Controller which opens after some navigation operations

firstly I am extracting all the folders here

 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Folder" inManagedObjectContext:self.managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    NSError *error = nil;
    m_folderResults = [self.managedObjectContext executeFetchRequest:request error:&error];

then I have created a new instance of file

File *fileObject = [NSEntityDescription insertNewObjectForEntityForName:@"File" inManagedObjectContext:self.managedObjectContext];
    fileObject = (passed file object to this ViewController)

Folder *folderObject = [m_folderResults objectAtIndex:m_indexPath.row];

    NSMutableOrderedSet *files = [folderObject mutableOrderedSetValueForKey:@"file"];
    [files addObject:fileObject];

This is working but the problem I am facing is that I am getting it this way

folder A B
file   a b
file     d
file     c

Meaning it is getting deleted from one folder and getting added to another folder.

So I want to know where I am going wrong.

Regards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • Seems that you not actually doing a file copy. I do not see here the code that caused this problem. Could you share some more code? – Mark Kryzhanouski Mar 14 '13 at 08:12
  • 1
    Could it be that in `fileObject = (passed file object to this ViewController)` you *replace* the newly created file object by the one that you want to copy? You probably have to copy the attributes instead, e.g. `fileObject.name = oldFileObject.name, ...`. – Martin R Mar 14 '13 at 08:32
  • Hi @MarkKryzhanouski, actually this is what I have written to copy one file from one folder to another, I dont have no more code to share – Ranjit Mar 14 '13 at 08:45
  • Ok, then clarify what does this mean `fileObject = (passed file object to this ViewController)`? – Mark Kryzhanouski Mar 14 '13 at 08:49
  • @Ranjit: So you *assign* a new value to `fileObject`? That will not copy the object. – Martin R Mar 14 '13 at 08:50
  • HI @MartinR, I tried as you suggested and its working. thanks – Ranjit Mar 14 '13 at 08:51
  • @Ranjit: You are welcome! - You already got an answer :-) – Martin R Mar 14 '13 at 08:55
  • Hi @MartinR, I have one more doubt, what if I first copied a file a to folder B then I moved file a to folder B, then I want to have two copies of file a in folder B. How this can be done – Ranjit Mar 14 '13 at 10:40
  • @Ranjit: "Copying file c" means that you create a new file object (file c') and copy the attributes from c to c'. These are two independent objects now (one in folder A and one in folder B). When you "move" the original object c from A to B later, you have c and c' in folder B. - (Try to think of Core Data as an "Object graph mananger": it manages objects and their relationships.) – Martin R Mar 14 '13 at 10:50

1 Answers1

1

First, according to the code you posted, you are overwriting the newly created file object with your old file object. Of course, if you insert that elsewhere it will be gone from where it was before.

Second, it should be clarified if you want to (1) really copy file c in the sense that you want to create a new instance and assign that to a different folder or if you (2) just want to have the second folder also point to the same file (which would make sense if the files do not change).

For case (1) you have to

File *fileToBeCopied; 
Folder *destinationFolder;
File *newFile = [NSEntityDescription insertNewObjectForEntityForName:@"File" 
                          inManagedObjectContext:self.managedObjectContext];

// now you need to copy all the attributes of fileToBeCopied
// over to newFile

[destinationFolder addFileObject:newFile];
// or
newFile.folder = destinationFolder;

For case (2), make sure your data model allows one file to have many folders (many-to-many relationship).

[destinationFolder addFileObject:fileToBeCopied];
// or
[fileToBeCopied addFolderObject:destinationFolder];
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Hi @Mundi, your second solution seems to be perfectly optimal, but as I have mentioned , that I have one-to-many realtionship, is it easy to change this to many to many? – Ranjit Mar 14 '13 at 08:57
  • Yes, just modify the model, regenerate the class files (which will give you the method `addFolderObject:`), delete the app from the simulator or testing device and you are ready to go. – Mundi Mar 14 '13 at 09:01
  • it will not break anything right, it will just give create a NSOrderedSet of Folders in .h files right? – Ranjit Mar 14 '13 at 09:11
  • That won't work. You will have to adjust the underlying core data model so the changes can be persisted in the database. – Mundi Mar 14 '13 at 13:10
  • Okay, it breaks a lot of my code, because, if I use many-to-many relationship , I get an NSOrdered set of folders, and I cannot access the folder with fileObject because it doesnt exist. – Ranjit Mar 14 '13 at 13:25