核心数据关系之谜

时间:2014-03-03 20:19:37

标签: ios objective-c uitableview core-data

我正在构建的简单应用程序,以揭示我的初学者对Core Data关系的理解中的差距就是这样做的。这是一个简短的描述:

该应用有三种观看结果:HomeViewControllerAddViewControllerAddCategoryViewController

我的数据模型有两个实体,关系如下:

enter image description here

通过WMDGCategory中的文字字段添加新的AddCategoryViewController个对象,并通过WMDGActivity中的文字字段添加新的AddViewController个对象。

我的目标是能够在HomeViewController的tableview中显示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

当我添加新活动并使用以下代码将其分配给现有类别时:

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

我的tableview中没有显示任何内容,我收到此错误调用堆栈:

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是自我

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;

1 个答案:

答案 0 :(得分:2)

虽然您设置了self.thisActivity.toCategory.name,但通常不应该这样做,而您实际上从未设置过self.thisActivity.toCategory

除非您创建对象,否则它们不存在。除非您设置对象,否则关系不包含对象。您的问题似乎是您实际上从未创建(或至少获得现有的)WMDGCategory实例,您可以将其连接到WMDGActivity

因此,当您尝试使用FRC中的关系内容时,您会得到nil。 (您可以在日志中看到它反复说toCategory = nil;)。

通过将您的活动toCategory设置为WMDGCategory个实例来解决此问题。