2012-02-12 150 views
0

我刚接触iOS开发并对核心数据做了一些测试。直到我开始玩关系之前,这一切都很好。 在上下文中:核心数据,一对多关系

我有2个实体:Article和Category。

文章有两个成员idArticle和文本。文章必须有一个类别。 类别有两个成员idCategory和name。类别可以有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

这可能发生的最可能的方式是如果您的文章实体是类别的子类。

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

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

当您创建文章时,您正在创建一个新类别,因为根据您的结构,文章is-a类别。

但是没有看到.xcdatamodel这个答案是一个猜测。