Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should be able to delete objects from the database using <code>NSManagedObjectContext</code>'s <code>deleteObject:</code> method. Similarly, if you have created .h and .m files from your core data model, it should have created methods for the object to remove one-to-many relationship objects from itself (something similar to <code>-(void)removeCustomersObject:(Customer *)value</code> or <code>-(void)removeCustomers:(NSSet *)value</code>.</p> <p>Using this information, you should be able to set up a parameter that checks if the balance is zero and removes the objects using one of these methods. One possibility is occasionally using a fetch request to find all customers with a zero balance (using <code>NSPredicate</code>) and removing the fetched results from your database. I hope this helps and gives you an idea where to go. I would have been more detailed, but it's hard to give too much advice without more information. Regardless, this should provide a good stepping stone on how to alleviate your problem.</p> <p>EDIT: There are two ways to go about deleting multiple objects from your Core Data. The simplest is assuming you have two entities. One is the parent, what I would refer to as <code>MyDatabase</code>, which holds all the children in a one-to-many relationship. These would be your Customer or Person objects. What you name it isn't too important, but those are the objects with a balance. For this example, I will just call the class <code>Customer</code>.</p> <p>If you generate code for the entities (by going to your xcdatamodeld file, selecting the entities, then go to Editor->Create NSManagedObject Subclass), you will see something like this for the parent entity.</p> <pre><code>@class Customer; @interface MyDatabase : NSManagedObject { } @property (nonatomic, retain) NSSet *customers; </code></pre> <p>and in your .m file,</p> <pre><code>@implementation MyDatabase @dynamic customers - (void)addCustomersObject:(Customer *)value { NSSet *changedObjects = [[NSSet alloc] initWithObjects:&amp;value count:1]; [self willChangeValueForKey:@"customers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects]; [[self primitiveValueForKey:@"customers"] addObject:value]; [self didChangeValueForKey:@"customers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects]; [changedObjects release]; } - (void)removeCustomersObject:(Customer *)value { NSSet *changedObjects = [[NSSet alloc] initWithObjects:&amp;value count:1]; [self willChangeValueForKey:@"givenSurveys" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects]; [[self primitiveValueForKey:@"customers"] removeObject:value]; [self didChangeValueForKey:@"customers" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects]; [changedObjects release]; } - (void)addCustomers:(NSSet *)value { [self willChangeValueForKey:@"customers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value]; [[self primitiveValueForKey:@"customers"] unionSet:value]; [self didChangeValueForKey:@"customers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value]; } - (void)removeCustomers:(NSSet *)value { [self willChangeValueForKey:@"customers" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value]; [[self primitiveValueForKey:@"customers"] minusSet:value]; [self didChangeValueForKey:@"customers" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value]; } </code></pre> <p>Again, these are created for you when the file is created, though if for some reason they aren't, you may not have set up the relationship properly.</p> <p>To remove the files, you would want to fetch the context using a predicate. The returned request is an <code>NSArray</code> with the objects that match the criteria (a balance of 0). For this example, we'll just call that array <code>objectsToRemove</code>. At this point, we're ready to go about both possible solutions.</p> <p>1) In this, you don't have a child/parent entity map for your Core Data and just have one entity. In that case, you will use your fetch request as described above. Then you will call something like the following</p> <pre><code>for (id myObject in objectsToDelete) { [self.managedObjectContext deleteObject:myObject]; } // commit the changes; NSError *error = nil; if (![self.managedObjectContext save:&amp;error]) { // handle the error; } </code></pre> <p>This deletes all the objects, then saves your context once done.</p> <p>2) If you do have a parent/child Core Data map, it gets easier. In this case, you will simply call the following during your fetch request</p> <pre><code>[myDatabase removeCustomers:[NSSet setWithArray:objectsToDelete]]; // commit the changes; NSError *error = nil; if (![self.managedObjectContext save:&amp;error]) { // handle the error; } </code></pre> <p>The second approach removes all the objects at once, but does require you have an entity that holds the <code>Customer</code> objects. In my opinion, it's a nicer approach, though not necessary, especially if you don't need to separate <code>Customer</code> objects into different groups. This <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NSPersistentDocumentTutorial/00_Introduction/introduction.html" rel="nofollow">Core Data tutorial</a> gives an overview of the classic Department and Employees project, which is a very simple, but clear example of how parent/child hierarchies are used in Core Data.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload