I new to iOS development and doing some test with the core data. It was all fine until I started playing with relationship.
In context:
I have 2 entity: Article and Category.
Article has two members idArticle and text. Articles has to have a single Category.
Category has two members idCategory and name. Category can have 0 or several Articles.
Article:
- idArticle
- text
- category (Relationship to Category, Inverse = articles, minimum optional, maximum 1)
Category:
- idCategory
- name
- articles (To many Relationship to Article, Inverse = category, minimum optional, maximum unlimited)
While I am adding an article. I am first surprise to not only have article1.category but also article1.idCategory and article1.name!
I have currently set all my attributes, relationship to optional.
Whatever I do, when I add a new article using the code below, it will add a new Category as well which will contain idCategory = 0 and name = nil if I dont set article.idCategory and article.name! or to the corresponding value if I set them. However, I don’t want it to create that category, I just want to add an existing category.
article.category works fine; it add the article to the right category! If I only set article.category; article.idCategory will = 0 and article.name = nil.
I suppose that I could delete the newly created category but I want my code to be neat. I have search the web but didnt find similar example with that problem. I am not dealing with any GUI here.
My code:
- (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;
}
and
-(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;
}
}
Thanks! This super basic task is currently ruining my sunday!
The most likely way this could have happened is if your Article Entity is a sub-class of Category.
You would get exceptions otherwise when setting
gmtArticle.idCategorytelling you thatGasMattersTodayArticlesis not key-value compliant toidCategory.GasMattersTodayArticlesandGasMattersTodayCategoryshould both be sub-classes ofNSManagedObject(or maybe a base class for your project).You are creating a new category when you create an article because according to your structure an Article is-a Category.
However without seeing the
.xcdatamodelthis answer is a guess.