Question

I'm trying to detect the amount of objects I have in Core Data when the user shakes the device. When I try and call a NSFetchRequest inside of motionEnded:, the simulator crashes with an unknown error at main.

Is doing a fetch like this inside of motionEnded: possible?

The code I have so far:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
        // see if we have albums to upload

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Groups" inManagedObjectContext:managedObjectContext];

        [fetchRequest setEntity:entity];

        NSUInteger group_count = [managedObjectContext countForFetchRequest:fetchRequest error:nil];

        if (group_count == 0)
        {
            // show alertview
        }
        else
        {
            // show another alertview
        }

}
Was it helpful?

Solution

This is perfectly fine. You can trigger a search and an alert view based on the search result triggered by a gesture, including a shake gesture.

Check what exactly your error is, so pass an error object to your countForFetchRequest method. Possibly your managedObjectContext is nil or you have some problem with your entity name. ("Groups" would be a bad name for an entity. "Group" is much more logical.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top