核心数据,一对多关系

时间:2012-02-12 15:58:57

标签: iphone ios ipad core-data

我是iOS开发新手并使用核心数据进行一些测试。在我开始玩关系之前一切都很好。 在上下文中:

我有2个实体:文章和类别。

文章有两个成员idArticle和文本。文章必须有一个类别。 类别有两个成员idCategory和名称。类别可以有0个或几个文章。

文章:

  • idArticle
  • 文本
  • 类别(与类别的关系,反向=文章,最小可选,最多1)

类别:

  • idCategory
  • 名称
  • 文章(与许多文章的关系,反向=类别,最小可选,最大无限制)

我正在添加一篇文章。我首先感到惊讶的是不仅有article1.category而且还有article1.idCategory和article1.name! 我目前已将所有属性,关系设置为可选。 无论我做什么,当我使用下面的代码添加新文章时,它将添加一个新的类别,如果我没有设置article.idCategory和article.name,它将包含idCategory = 0和name = nil!或者如果我设置它们的相应值。但是,我不希望它创建该类别,我只想添加现有类别。 article.category工作正常;它将文章添加到正确的类别!如果我只设置article.category; article.idCategory将= 0且article.name = nil。

我想我可以删除新创建的类别,但我希望我的代码整洁。我在网上搜索但没有找到与该问题类似的例子。我这里没有处理任何GUI。 我的代码:

 - (BOOL)createNewGmtArticle:(NSNumber*)articleID title:(NSString*)paramTitle text:(NSString*)paramText date:(NSDate*)paramDate categoryID:(NSNumber*)paramCategoryID categoryName:(NSString*)paramCategoryName

{
    GasMattersTodayArticles *gmtArticle = [NSEntityDescription insertNewObjectForEntityForName:@"GasMattersTodayArticles" inManagedObjectContext:self.managedObjectContext]; // Look the given entitiy GasMattersTodayArticles in the given managed obj context

    if(gmtArticle != nil)
    {
        // Fill article
        gmtArticle.idArticle = articleID;
        gmtArticle.text = paramText;

        gmtArticle.category = [self getGmtCategoryWithId:paramCategoryID];
        //gmtArticle.idCategory = gmtArticle.category.idCategory; 
        //gmtArticle.name = gmtArticle.category.name;        

        NSError *savingError = nil;
        if([self.managedObjectContext save:&savingError]) // flush all unsaved data of the context to the persistent store
        {
            NSLog(@"Successfully saved the context");
            return YES;
        }
        else
        {
            NSLog(@"Failed to save the context. Error = %@", savingError);
            return NO;
        }
    }

    NSLog(@"Failed to create the new article");
    return NO;
}

-(GasMattersTodayCategory *)getGmtCategoryWithId:(NSNumber*)categoryID

{
    // Create the fetch request first
    NSDictionary *subs = [NSDictionary dictionaryWithObject:categoryID forKey:@"SEARCH_KEY"];    
    NSFetchRequest *fetchRequest = [self.managedObjectModel fetchRequestFromTemplateWithName:@"CategoryWithKey" substitutionVariables:subs];
    // Entity whose contents we want to read
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"GasMattersTodayCategory" inManagedObjectContext:self.managedObjectContext];

    // Tell the request that we want to read the content of the person entity
    [fetchRequest setEntity:entity];

    // Excecute the fetch request on the context
    NSError* requestError = nil;
    GasMattersTodayCategory *category = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&requestError] lastObject];

    // Make sur we get a category
    if(category != nil)
    {
        return category;
    }
    else
    {
        NSLog(@"Could not find any Category entities with this Id in the context.");
        return nil;
    }    
}

谢谢!这个超级基本任务目前正在破坏我的星期天!

1 个答案:

答案 0 :(得分:1)

最有可能发生这种情况的方法是,如果您的文章实体是类别的子类。

当设置gmtArticle.idCategory告诉您GasMattersTodayArticles不符合idCategory的键值时,您会收到例外情况。

GasMattersTodayArticlesGasMattersTodayCategory都应该是NSManagedObject的子类(或者可能是项目的基类)。

您在创建文章时创建新类别,因为根据您的结构,文章是-a 类别。

然而,如果没有看到.xcdatamodel这个答案就是猜测。