在我的应用程序中,我想创建它,以便当用户打开应用程序且没有对象时,我希望它向表视图的第一部分添加一个对象。我在我的应用程序委托中知道以下方法:
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是否为空?
发布评论