How to Clear/Reset All Coredata in One-To-Many Relationship

How to clear/reset all CoreData in one-to-many relationship

I have solved this problem, below is the code,

This function has been written in appdelegate.m

- (void) resetApplicationModel
{
NSError *error;
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AppName.sqlite"];
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
for (NSManagedObject *ct in [self.managedObjectContext registeredObjects]) {
[self.managedObjectContext deleteObject:ct];
}

//Make new persistent store for future saves
if (![self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// do something with the error
}
}

And in my SettingsViewController, I am calling this on resetbutton clicked in this way.

- (void)resetButtonclicked
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate resetApplicationModel];
}

Regards
Ranjit.

Swift core data removing entries in one to many relationships

I found that in relation one to many Account -> User without inverse relationship in User, after changing list in Account every user in list have to be marked that has changed (property updated == true). But it always was set to false.

I added additional property to User, and after every change of list I had to change this property like user.toUpdate = true. After this change everything is working fine.

Core data delete all relationship entity

Using the example you looked at above linked as "this one".

If you use a predicate that filters the Employees by checking the relationship to Department is nil, that'd return just the data items you want. Then I suggest you could delete all of them.

Core Data delete rules, delete all relationships and entities (many to many)

The delete rule you are looking for is Cascade

If you want a behavior wherein on deletion of House all other entities are deleted, then you'll need to setup Cascade delete rule on the both ends of the relationships.

Deleting object from one to many relationship in Core Data

Okay, so I found a work around for this very weird problem.
Since the autogenerated accessor methods didn't work for some reason, I had to think of another way to delete the object from the relationship.

          for (Product *prod in _cart.products) {

//To Very reliable to check for item by name attribute, but It works :)
if ([prod.name isEqualToString:product.name]) {

product = prod;

NSMutableSet *set = [NSMutableSet setWithSet:_cart.products];
[set removeObject:prod];
_cart.products = set;

}

}

Core Data: Delete the last entity with one to many relationship

If you want to delete an Activities when it has no more ActivityRecords then... you have to delete the Activities. There is no way to automate this in Core Data. If you don't want to have any Activities objects with no related ActivityRecords, then you need to write your own code that deletes them.

When you're going to delete an ActivityRecord, you need to check the related Activities. If it doesn't have any more activity records, delete it too.

How to delete all objects through a core data relationship?

If you want to delete the employee elements for a specific department, then you could run a for-in loop like for

for (Employees * theEmployee in department.employees) {
[self.managedObjectContext deleteObject:[self.managedObjectContext objectWithID:theEmployee.objectID]];
}

Then save your managed context. IF of course that's what you want, and not remove the relationship between employees and departement; in that case, assigning an empty set would work.

Variation on above:

for (Employee *employeeToDelete in department.employees) {
[self.managedObjectContext deleteObject:employeeToDelete];
}

Core Data delete rule -- to-many relationship, delete when empty

You could override prepareForDeletion in your Book class and check if the author has any other books. If not you could delete the author.

- (void)prepareForDeletion {
Author *author = self.author;
if (author.books.count == 1) { // only the book itself
[self.managedObjectContext deleteObject:author];
}
}

Edit: To prevent deletion of an author with books you could override validateForDelete or even better: don't call deleteObject with an author with books in the first place

Core Data: Quickest way to delete all instances of an entity

iOS 9 and later:

iOS 9 added a new class called NSBatchDeleteRequest that allows you to easily delete objects matching a predicate without having to load them all in to memory. Here's how you'd use it:

Swift 5

let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Car")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

do {
try myPersistentStoreCoordinator.execute(deleteRequest, with: myContext)
} catch let error as NSError {
// TODO: handle the error
}

Objective-C

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Car"];
NSBatchDeleteRequest *delete = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];

NSError *deleteError = nil;
[myPersistentStoreCoordinator executeRequest:delete withContext:myContext error:&deleteError];

More information about batch deletions can be found in the "What's New in Core Data" session from WWDC 2015 (starting at ~14:10).

iOS 8 and earlier:

Fetch 'em all and delete 'em all:

NSFetchRequest *allCars = [[NSFetchRequest alloc] init];
[allCars setEntity:[NSEntityDescription entityForName:@"Car" inManagedObjectContext:myContext]];
[allCars setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSError *error = nil;
NSArray *cars = [myContext executeFetchRequest:allCars error:&error];
[allCars release];
//error handling goes here
for (NSManagedObject *car in cars) {
[myContext deleteObject:car];
}
NSError *saveError = nil;
[myContext save:&saveError];
//more error handling here


Related Topics



Leave a reply



Submit