2012-02-12 269 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這個答案是一個猜測。