2012-02-12 35 views
0

iOS開発の初心者で、コアデータでいくつかのテストを行っています。私が関係を持って遊ぶようになるまでは大丈夫でした。 コンテキスト内:コアデータ、一対多リレーションシップ

私は2つのエンティティ:記事とカテゴリを持っています。

Articleには、idArticleとtextの2つのメンバーがあります。記事には単一のカテゴリが必要です。 カテゴリには2つのメンバー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

あなたのArticle EntityがCategoryのサブクラスである場合、この可能性が最も高い方法です。

gmtArticle.idCategoryGasMattersTodayArticlesidCategoryに準拠していないことを通知すると例外が発生します。

GasMattersTodayArticlesGasMattersTodayCategoryの両方がNSManagedObjectのサブクラス(または、おそらくプロジェクトの基本クラス)である必要があります。

記事を作成する際に、新しいカテゴリを作成するのは、構造に応じて、記事が-カテゴリであるためです。

.xcdatamodelを見ることなく、この回答は推測です。

関連する問題