检查managedobjectcontext是否为空? [英] Check if managedobjectcontext is empty?

查看:85
本文介绍了检查managedobjectcontext是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想创建它,以便当用户打开应用程序且没有对象时,我希望它向表视图的第一部分添加一个对象。我在我的应用程序委托中知道以下方法:

In my application I want to make it so that when the user opens the application and there are no objects, i want it to add an object to the first section of the table view. I know in my App Delegate in the method:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

我可以做这样的事情:

if (XXXXXX){    
NSManagedObjectContext *context = self.managedObjectContext;
        NSManagedObject *startingTask = [NSEntityDescription insertNewObjectForEntityForName:@"Tasks" inManagedObjectContext:context];
        [startingTask setValue:@"Eat Dinner" forKey:@"taskName"];
        [startingTask setValue:[NSNumber numberWithDouble:400] forKey:@"timeInterval"];
        [startingTask setValue:@"Tasks To Complete" forKey:@"sectionString"];
}

其中XXXXX检查managedobjectcontext是否为空(或者基本上没有对象)

where XXXXX checks whether the managedobjectcontext is empty (or basically, there are no objects to fetch).

,但是XXXXX是什么?

but what would be XXXXX?

推荐答案

只需执行通常执行的获取操作即可填充表。如果未返回任何记录,则存储出于您的目的而为空。这就是您真正想知道的问题。

Just perform the fetch you would normally perform to fill your table. If it returns no records, then the store is empty for your purposes. That's the question you really want to know about.

同一持久性存储中可能存在与表记录不相关的记录,所以不要不必担心商店中此类问题的确切总数。 (不要以为您的模型不会改变;将独立实体放入商店是完全合法的。)

It's possible that there might be records in the same persistent store that aren't related to your table records, so don't worry about the exact number of total entities in the store for this kind of problem. (Don't assume you model will never change; it's complely legal to put independent entities into a store.)

这篇关于检查managedobjectcontext是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆