私のアプリケーションでは、CategoryエンティティとSub_Categoryエンティティがあり、それらの間に多対多の関係があります。
まず、アプリを初めて実行すると、リレーション属性(category_subCategory)が次のように機能します。
2012-04-11 04:03:39.344 SupplyTrackerApp[27031:12f03] Saved
2012-04-11 04:03:47.423 SupplyTrackerApp[27031:fb03] Category<Category: 0x6ba4e90> (entity: Category; id: 0x6e95620 <x-coredata://00E5784E-D032-41DD-BD60-B85B0BBF8E31/Category/p1> ; data: {
categoryID = 1;
categoryName = Antiques;
"category_SubCategory" = (
"0x6ebb4f0 <x-coredata://00E5784E-D032-41DD-BD60-B85B0BBF8E31/Sub_Category/p1>"
);
catetogoryDescription = "Contains a Sub categories related to Antique pieces";
})
しかし、アプリを終了してアプリケーションを再起動すると、その関係は存在します。出力は次のようになります。
2012-04-11 04:05:04.455 SupplyTrackerApp[27038:fb03] In Cell for row at index path
2012-04-11 04:05:05.548 SupplyTrackerApp[27038:fb03] Category<Category: 0x6ecaca0> (entity: Category; id: 0x6eb4ab0 <x-coredata://00E5784E-D032-41DD-BD60-B85B0BBF8E31/Category/p1> ; data: {
categoryID = 1;
categoryName = Antiques;
"category_SubCategory" = "<relationship fault: 0x6ecf0a0 'category_SubCategory'>";
catetogoryDescription = "Contains a Sub categories related to Antique pieces";
})
これが私がcreateentitiesと呼んでいるところです。
-(void) loadDataIntoDocument{
NSLog(@"In Load Data");
dispatch_queue_t fetch=dispatch_queue_create("Data Fetcher", NULL);
dispatch_async(fetch, ^{
[Sub_Category createSubCategory:context];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}else {
NSLog(@"Saved");
}
});
dispatch_release(fetch);
}
したがって、Sub_Category +CreateCategoryファイルには次のコードがあります。
+(Sub_Category *) createSubCategory:(NSManagedObjectContext *)context{
Sub_Category *subCategory=nil;
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Sub_Category"];
request.predicate=[NSPredicate predicateWithFormat:@"subCategoryID=%@", @"11"];
NSSortDescriptor *sortDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"subCategoryName" ascending:YES];
request.sortDescriptors=[NSArray arrayWithObject:sortDescriptor];
NSError *error;
NSArray *matches=[context executeFetchRequest:request error:&error];
if (!matches||[matches count]>1) {
///errorrrrrrrrrr
} else if([matches count]==0) {
subCategory=[NSEntityDescription insertNewObjectForEntityForName:@"Sub_Category" inManagedObjectContext:context];
subCategory.subCategoryID=[NSString stringWithFormat:@"%i", 11];
subCategory.subCategoryName=@"Antiquities";
subCategory.subCategoryDescription=@"Contains several products related to antiquities";
subCategory.subCategory_Category =[Category createCategory:context];
}else {
subCategory=[matches lastObject];
}
return subCategory;
}
そして、次のコードを持つCategory +CreateCategoryファイルを呼び出しています。
+(Category *) createCategory:(NSManagedObjectContext *)context{
Category *category=nil;
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Category"];
request.predicate=[NSPredicate predicateWithFormat:@"categoryID=%@", @"1"];
NSSortDescriptor *sortDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"categoryName" ascending:YES];
request.sortDescriptors=[NSArray arrayWithObject:sortDescriptor];
NSError *error;
NSArray *matches=[context executeFetchRequest:request error:&error];
if (!matches||[matches count]>1) {
///errorrrrrrrrrr
} else if([matches count]==0) {
category=[NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:context];
category.categoryID=[NSString stringWithFormat:@"%d",1];
category.categoryName=@"Antiques";
category.catetogoryDescription=@"Contains a Sub categories related to Antique pieces";
}else {
category=[matches lastObject];
}
return category;
}
誰かがこれに関して私を助けることができますか。
私は数日からこれに固執しています...