在我的应用程序中,我有Category和Sub_Category实体,它们之间有很多关系。
首先,当App第一次运行时,关系属性(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";
})
这是我正在调用创建实体的地方..
-(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 + Create Category文件具有以下代码。
+(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 + Create Category文件,其中包含以下代码。
+(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;
}
任何人都可以帮我解决这个问题。
我几天就坚持这个......
答案 0 :(得分:1)
我看起来您正在使用dispatch_async
在后台执行-save:
。这可能违反了处理NSManagedObject
的“线程限制”规则(简而言之,它们只应在它们创建的线程中使用。同样适用于从它们获取的任何对象)。
有关如何与Core Data进行并发的更多信息,请参阅this WWDC Session。
尝试仅使用主线程中的Core Data堆栈(以及从中获取的任何对象)。