0

当用户摇动设备时,我试图检测我在 Core Data 中拥有的对象数量。当我尝试在 motionEnded: 中调用 NSFetchRequest 时,模拟器在 main 处因未知错误而崩溃。

在motionEnded内部进行这样的提取:可能吗?

我到目前为止的代码:

- (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
        }

}
4

1 回答 1

0

这很好。您可以根据手势(包括摇晃手势)触发的搜索结果触发搜索和警报视图。

检查您的错误到底是什么,因此将错误对象传递给您的countForFetchRequest方法。可能你managedObjectContext是 nil 或者你的实体名称有问题。(“组”对于实体来说是一个坏名字。“组”更合乎逻辑。)

于 2013-11-21T07:28:09.100 回答