Master data deletes object functions

I have questions related to Core Data and object deletion functions that are automatically created when an NSManagedObject subclass is created for a specific object. I have entities; one is called Label, with one relationship called artists, and another object called Artist, which has one relationship called label. the artists destination is an Artist object with the inverse relationship set for the label.

I have added several "Artist" with the same name (Freekey Zekey) and I am trying to remove them using the following code

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

// Grab the artist and delete
Artist *freekey = [fetchedObjects objectAtIndex:0];
[freekey.label remove ArtistsObject:freekey];


// Save everything
if([context save:&error])
{
    NSLog(@"The save was successful!");
}
else
{
    NSLog(@"The save wasn't successful: %@", [error localizedDescription]);
}

      

After executing this code, I read the data, but the artist is only deleted when I only have one "Freekey Zekey". However, when I run the application again, the number of objects in fetchedObjects remains the same. If I have more than one "Freekey Zekey", I expect only one to be removed, but it won't.

I tried to use this code to remove all "Freekey Zekey"

for(int i=0; i < [fetchedObjects count]; i++)
{
    freekey = [fetchedObjects objectAtIndex:i];

    [freekey.label remove ArtistsObject:freekey];
}

      

And it works, but when I check the number of objects in fetchedObjects it is not 0, which I believe means the objects were not deleted.

How do I completely remove an object?

+3


source to share


2 answers


Answer: you are not completely removing artists from the database. You just deleted the relationship and you need to delete the artists in order to perform the deletion.

for(int i=0; i < [fetchedObjects count]; i++)
{
    freekey = [fetchedObjects objectAtIndex:i];
    [freekey.label removeArtistsObject:freekey];
    [context deleteObject:freekey];
}
if([context save:&error])
{
    NSLog(@"The save was successful!");
}
else
{
    NSLog(@"The save wasn't successful: %@", [error localizedDescription]);
}

      



Hope it helps.

+3


source


Apple docs are actually not entirely clear. General Method: Apple advises:

Data *lastData = [[self sortedPersonDatas] objectAtIndex:0];
[selectedPerson removePersonDataObject:lastData];

      

where PersonData is a one-to-many relationship to a data-driven object from I took the last data (lastData obtained from a sorted array of all data) But with the factory drop methods and check the SQL database behind we can detect that actual data exists , only the inverse relation is null. To completely remove data (all attributes and object), you had to use:



[selectedPerson.managedObjectContext deleteObject:lastData];

      

this will completely delete the entries while maintaining the context. Correct documentation from Apple

+2


source







All Articles