Why doesn't the prepareForDeletion: method get called?
I'm deleting an object using its ancestor relationship:
[folder removeDocumentObject: doc];
The "doc" object gets deleted like expected, but its prepareForDeletion method never gets called... =\
A delete rule defines what happens when the record that owns the relationship is deleted. A delete rule defines what happens when the record that owns the relationship is deleted. Select the notes relationship of the Category entity and open the Data Model Inspector on the right.
Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that: Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.
The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.
Open Xcode and create a new iOS project based on the Single View App template. Name the app HitList and make sure Use Core Data is checked. Checking the Use Core Data box will cause Xcode to generate boilerplate code for what's known as an NSPersistentContainer in AppDelegate.
Because it's not being deleted :)
If [folder documentObject]
returns nil that just means that there is no relationship between the two objects. It doesn't mean that the object is deleted from core data.
Try this :
[[doc managedObjectContext] deleteObject:doc];
EDIT
Using this code I have tested your problem :
I have made a new project with a Document and Folder object in Core Data - it's set to folder <<--> document with cascasde set on the folder relationship.
// Create a document and folder and save them
Folder *fol = [NSEntityDescription insertNewObjectForEntityForName:@"Folder" inManagedObjectContext:[self managedObjectContext]];
Document *doc = [NSEntityDescription insertNewObjectForEntityForName:@"Document" inManagedObjectContext:[self managedObjectContext]];
[doc setFolder:fol];
[[self managedObjectContext] save:nil];
// Get the document's object id for later
NSManagedObjectID *documentId = [doc objectID];
// Delete the relationship
[fol removeDocumentsObject:doc];
[[self managedObjectContext] save:nil];
// Try to reload the document to see if it's still there
Document *doc2 = (Document *)[[self managedObjectContext] objectWithID:documentId];
// Get a property from the document - this will fault if the document has been deleted. Otherwise it
// will work.
NSLog(@"%@", doc2);
NSLog(@"%@", [doc2 title]);
// Now delete specifically and try again
[[self managedObjectContext] deleteObject:doc];
[[self managedObjectContext] save:nil];
// Should get a fault here
doc2 = (Document *)[[self managedObjectContext] objectWithID:documentId];
NSLog(@"%@", doc2);
NSLog(@"%@", [doc2 title]);
The first two NSLog statements work correctly - I get a description of the document and a string for the title.
The second NSLog statements crash because the document doesn't exist in the store any more.
This tells me that just removing a relationship isn't enough to remove the Document from the store - you have to explicitly delete it. However, you say that you think it's deleted. Can you post the query that you are running to see if it's still in the store or not?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With