简体   繁体   中英

how to delete objects in core data

I am trying to understand how core data works. So I have in my core data 2 entities : Voiture and Garage (yes, i'm french :) )

I can create objects but i can't delete them ! I tried everything… It would be nice to help me a bit !

here is my code :

@interface dataBaseViewController ()

@property(strong,nonatomic) UIManagedDocument *document;
@property(strong,nonatomic) NSManagedObjectContext *context;

@end

@implementation dataBaseViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a

[self initDocument];

self.context=self.document.managedObjectContext;

}


-(void) initDocument{



//find url
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectory=[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSString *documentName=@"MyDocument";
NSURL *url= [documentsDirectory URLByAppendingPathComponent:documentName];


 //create / open the document
    self.document = [[UIManagedDocument alloc] initWithFileURL:url] ;

    if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {

        [self.document openWithCompletionHandler:^(BOOL success) {
                if (success) NSLog(@"doc ouvert");
                  if (!success) NSLog(@"couldn’t open document at %@", url);
              }];

        } else {

            [self.document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
                if (success)        NSLog(@"document created");
              if (!success) NSLog(@"couldn’t create document at %@", url);
        }];
        }
}



- (IBAction)ajouterVoiture:(id)sender {
Voiture *nouvelleVoiture =[NSEntityDescription insertNewObjectForEntityForName:@"Voiture" inManagedObjectContext:self.context];
nouvelleVoiture.marque=@"ferreri";

}
- (IBAction)nbVoitures:(id)sender {
NSError *error;
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Voiture"];

NSLog(@"nombre de voitures : %lu",[self.context countForFetchRequest:request error:&error]);
}


- (IBAction)delete:(id)sender {
   [self.context deletedObjects];
   NSError *error;
   [self.context save:&error];
}


@end

Once you have fetched a managed object, you can delete it from its managed object context by using the deleteObject: method provided by the context.

NSManagedObject *someObject;

[context deleteObject:someObject];

The object will not be removed from the underlying persistent store on disk until the context is saved, using the save: method.

I highly recommend the Magical Record pod. It greatly simplifies boilerplate Core Data operations.

[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
    MyEntity *entity = [MyEntity MR_createInContext:localContext];
    MyOtherEntity *otherEntity = [MyOtherEntity MR_findFirstByAttribute:@"id" withValue:12345];

    entity.attribute1 = @"Foo";
    entity.attribute2 = @"Bar";

    [otherEntity deleteInContext:localContext];
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM