我正在构建的一个简单应用程序就是这样做,它旨在揭示我的初学者对核心数据关系的理解。这里是一个简短的描述:
该应用程序具有三个视图:HomeViewController
,AddViewController
和AddCategoryViewController
。
我的数据模型有两个实体,它们之间的关系如下:
通过WMDGCategory
中的文本字段添加新的AddCategoryViewController
对象,并通过WMDGActivity
中的文本字段添加新的AddViewController
对象。
我的目标是能够在HomeViewController的表视图中显示WMDGActivity对象,该对象按WMDGCategory对象定义的部分分组。
当前,当我在sim卡中启动它时,没有崩溃,但是我得到了这个错误堆栈:
2014-03-03 12:39:07.893 EntitiesAndRelationships[323:a0b] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0x2f80ac) -> Created Context UNNAMED
2014-03-03 12:39:07.895 EntitiesAndRelationships[323:a0b] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x2f80ac) Set Root Saving Context: <NSManagedObjectContext: 0x8dadea0>
2014-03-03 12:39:07.899 EntitiesAndRelationships[323:a0b] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x2f80ac) Created Main Queue Context: <NSManagedObjectContext: 0x8aa4180>
2014-03-03 12:39:07.900 EntitiesAndRelationships[323:a0b] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x2f80ac) Set Default Context: <NSManagedObjectContext: 0x8aa4180>
2014-03-03 12:39:07.916 EntitiesAndRelationships[323:a0b] CoreData: error: (NSFetchedResultsController) object <WMDGActivity: 0x8aabff0> (entity: WMDGActivity; id: 0x8aac930 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGActivity/p1> ; data: {
category = nil;
name = Programming;
toCategory = nil;
}) returned nil value for section name key path 'toCategory.name'. Object will be placed in unnamed section
2014-03-03 12:39:07.917 EntitiesAndRelationships[323:a0b] CoreData: error: (NSFetchedResultsController) object <WMDGActivity: 0x8aad520> (entity: WMDGActivity; id: 0x8aac940 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGActivity/p2> ; data: {
category = nil;
name = "New Workplace Activity";
toCategory = nil;
}) returned nil value for section name key path 'toCategory.name'. Object will be placed in unnamed section
当我添加新 Activity 并将其分配给使用以下代码的现有类别时:
- (IBAction)saveButton:(UIBarButtonItem *)sender
{
if (self.activityField.text.length > 0)
{
if (self.categoryLabel.text.length < 1)
{
self.thisCategory.name = @"Uncategorized";
}
else
{
self.thisCategory.name = self.categoryLabel.text;
self.thisActivity.name = self.activityField.text;
self.thisActivity.toCategory.name = self.categoryLabel.text;
NSLog(@"Category name is %@", self.thisCategory.name);
NSLog(@"Activity name is %@", self.thisActivity.name);
}
[self.delegate addViewControllerDidSave];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No activity entered"
message:@"Please enter a new activity or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
我的表视图中没有任何显示,并且出现了此错误调用堆栈:
2014-03-03 13:08:24.321 EntitiesAndRelationships[323:a0b] Number of categories is 2
2014-03-03 13:08:24.323 EntitiesAndRelationships[323:a0b] Number of categories is 2
2014-03-03 13:08:24.328 EntitiesAndRelationships[323:a0b] Number of categories is 2
2014-03-03 13:08:24.329 EntitiesAndRelationships[323:a0b] Number of categories is 2
2014-03-03 13:08:24.333 EntitiesAndRelationships[323:a0b] Title of row is <WMDGCategory: 0x8ac0c20> (entity: WMDGCategory; id: 0x8ac4640 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGCategory/p2> ; data: <fault>)
2014-03-03 13:08:24.335 EntitiesAndRelationships[323:a0b] Title of row is <WMDGCategory: 0x8ac0e00> (entity: WMDGCategory; id: 0x8ac4730 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGCategory/p1> ; data: <fault>)
2014-03-03 13:08:24.338 EntitiesAndRelationships[323:a0b] Title of row is <WMDGCategory: 0x8ac0c20> (entity: WMDGCategory; id: 0x8ac4640 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGCategory/p2> ; data: {
activities = "<relationship fault: 0x8ad7ca0 'activities'>";
name = Sports;
})
2014-03-03 13:08:24.339 EntitiesAndRelationships[323:a0b] Title of row is <WMDGCategory: 0x8ac0e00> (entity: WMDGCategory; id: 0x8ac4730 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGCategory/p1> ; data: {
activities = "<relationship fault: 0x8dc0520 'activities'>";
name = WORKPLACE;
})
2014-03-03 13:08:24.342 EntitiesAndRelationships[323:a0b] Title of row is <WMDGCategory: 0x8ac0c20> (entity: WMDGCategory; id: 0x8ac4640 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGCategory/p2> ; data: {
activities = "<relationship fault: 0x8ad7ca0 'activities'>";
name = Sports;
})
2014-03-03 13:08:29.393 EntitiesAndRelationships[323:a0b] Title of row is <WMDGCategory: 0x8ac0e00> (entity: WMDGCategory; id: 0x8ac4730 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGCategory/p1> ; data: {
activities = "<relationship fault: 0x8dc0520 'activities'>";
name = WORKPLACE;
})
2014-03-03 13:12:08.187 EntitiesAndRelationships[323:a0b] Category name is (null)
2014-03-03 13:12:08.187 EntitiesAndRelationships[323:a0b] Activity name is Meeting
2014-03-03 13:12:11.077 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8aa4180) → Saving <NSManagedObjectContext (0x8aa4180): *** DEFAULT ***> on *** MAIN THREAD ***
2014-03-03 13:12:11.077 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8aa4180) → Save Parents? 1
2014-03-03 13:12:11.078 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8aa4180) → Save Synchronously? 1
2014-03-03 13:12:11.078 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8aa4180) Context DEFAULT is about to save. Obtaining permanent IDs for new 1 inserted objects
2014-03-03 13:12:11.081 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8dadea0) → Saving <NSManagedObjectContext (0x8dadea0): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2014-03-03 13:12:11.082 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8dadea0) → Save Parents? 1
2014-03-03 13:12:11.082 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8dadea0) → Save Synchronously? 1
2014-03-03 13:12:11.083 EntitiesAndRelationships[323:a0b] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8dadea0) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 1 inserted objects
2014-03-03 13:12:11.085 EntitiesAndRelationships[323:a0b] __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke25(0x8dadea0) → Finished saving: <NSManagedObjectContext (0x8dadea0): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2014-03-03 13:12:11.088 EntitiesAndRelationships[323:a0b] CoreData: error: (NSFetchedResultsController) object <WMDGActivity: 0x8aabff0> (entity: WMDGActivity; id: 0x8aac930 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGActivity/p1> ; data: {
category = nil;
name = Programming;
toCategory = nil;
}) returned nil value for section name key path 'toCategory.name'. Object will be placed in unnamed section
2014-03-03 13:12:11.088 EntitiesAndRelationships[323:a0b] CoreData: error: (NSFetchedResultsController) object <WMDGActivity: 0x8aad520> (entity: WMDGActivity; id: 0x8aac940 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGActivity/p2> ; data: {
category = nil;
name = "New Workplace Activity";
toCategory = nil;
}) returned nil value for section name key path 'toCategory.name'. Object will be placed in unnamed section
2014-03-03 13:12:11.097 EntitiesAndRelationships[323:a0b] CoreData: error: (NSFetchedResultsController) object <WMDGActivity: 0x8a87970> (entity: WMDGActivity; id: 0x8acabc0 <x-coredata://126C2FFD-8184-43B1-AA4D-AEFBE1AAC1A1/WMDGActivity/p3> ; data: {
category = nil;
name = Meeting;
toCategory = nil;
}) returned nil value for section name key path 'toCategory.name'. Object will be placed in unnamed section
我知道这很长,非常感谢您的宝贵时间来帮助我。我致力于理解这一点。
谢谢!
编辑-通过Wain 回答以下答复
我这样修改了我的代码,我认为这可以解决您的两个问题,即
1)
thisCategory
是self的WMDGCategory
属性2)我认为下面
else
语句中的修改后的代码将解决toCategory
问题- (IBAction)saveButton:(UIBarButtonItem *)sender
{
if (self.activityField.text.length > 0)
{
if (self.categoryLabel.text.length < 1)
{
self.thisCategory.name = @"Uncategorized";
}
else
{
self.thisCategory.name = self.categoryLabel.text;
self.thisActivity.name = self.activityField.text;
self.thisActivity.toCategory.name = self.thisCategory.name;
NSLog(@"Category name is %@", self.thisCategory.name);
NSLog(@"Activity name is %@", self.thisActivity.name);
}
[self.delegate addViewControllerDidSave];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No activity entered"
message:@"Please enter a new activity or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
但是,我仍然得到相同的读出值:
category = nil;
name = Zoomba;
toCategory = nil;
最佳答案
尽管设置了self.thisActivity.toCategory.name
,但通常不应该这样做,也永远不会真正设置self.thisActivity.toCategory
。
除非创建对象,否则对象不存在。关系除非包含对象,否则不包含对象。您的问题似乎是您从未真正创建(或至少获得一个现有的)可以连接到WMDGCategory
的WMDGActivity
实例。
因此,当您尝试在FRC中使用关系内容时,会得到nil
。 (您可以在日志中看到它反复显示toCategory = nil;
)。
通过将 Activity 的toCategory
设置为WMDGCategory
实例来解决此问题。