Delete objects from NSManagedObjectContext from another method

前端 未结 1 1054
余生分开走
余生分开走 2021-01-28 18:32

I\'m trying to delete all the objects, but I can\'t seem to make it work. I know there is other questions for this kind of question, but they are not helpful. What I want is to

相关标签:
1条回答
  • 2021-01-28 18:49

    So, what have you tried so far?

    As per the documentation.

    Deleting a managed object is straightforward. You simply send its managed object context a deleteObject: message, passing the object you want to delete as the argument.

    In other words, using a valid context you shoul say

    [aValidContext deleteObject:managedObjectYouWantToDelete];
    

    About your question, you need to

    1. Setup a fetch request to grab the Fixture items
    2. Loop the results (an NSArray) and perform the deletion

    For example

    NSFetchRequest* request = // create the request here...
    
    NSError *error;
    NSArray *array = [aValidContext executeFetchRequest:request error:&error];
    if (array == nil) {
        // Deal with error...
    } else {
        // loop the array performing deleteObject
    }
    
    // save to persistent store
    

    Remaining code is left for exercise ;)

    Edit 1

    Reading again your question, what is the relationship between Section and Fixture? If they are separated entities, you should perform previous steps also for Sections.

    0 讨论(0)
提交回复
热议问题