Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data: deleteObject crashes application, NSPredicate the cause?
    primarykey
    data
    text
    <p>I'm using a UICollectionView along with Core Data.</p> <p>I can't seem to figure out why I can't remove an object from Core Data. It crashes at <code>[self.managedObjectContext deleteObject:animation];</code> with an <code>index 0 beyond bounds for empty array</code> error. The following method is a delegate method that gets invoked from another viewcontroller. It gets a timestamp NSInteger and should create an NSPredicate with that timestamp.</p> <p>The strange thing is that when I take the animationTimestamp NSInteger and hardcode it like: <code>[NSPredicate predicateWithFormat:@"timestamp == %i", @"1370169109"]</code> it works and the object gets deleted without the error. However, when I want to pass the parameter as the argument it crashes. I've tried making it a string, NSNumber etc. Nothing works when I take the parameter, it only does when I hardcode it. I've logged animationTimestamp and it shows the correct value, so I'm stuck.</p> <p>Thanks!</p> <pre><code>- (void)deleteAnimationWithTimestamp:(NSInteger)animationTimestamp { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Animations" inManagedObjectContext:self.managedObjectContext]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"timestamp == %i", animationTimestamp]; [fetchRequest setPredicate:predicate]; [fetchRequest setEntity:entity]; NSError *error; NSArray *animations = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;error]; for (NSManagedObject *animation in animations) { [self.managedObjectContext deleteObject:animation]; } // Save core data if (![self.managedObjectContext save:&amp;error]) { NSLog(@"Couldn't save: %@", [error localizedDescription]); } [self.animationsCollectionView reloadData]; } </code></pre> <p>EDIT: More info that might matter. The timestamp attribute in the model is a 64 bit integer (should it be by the way?). When I create the object I use:</p> <pre><code>NSNumber *timestampNumber = [NSNumber numberWithInt:timestamp]; [newAnimation setValue:timestampNumber forKey:@"timestamp"]; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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