-1

在我的应用程序中,我有 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";
})

这是我调用创建实体的地方..

-(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;

}

任何人都可以帮我解决这个问题..

从几天开始我就被困在这个问题上......

4

1 回答 1

1

我看起来像你在后台dispatch_async做的。-save:这可能违反了处理 an 的“线程限制”规则NSManagedObject(简而言之,它们只能在创建它们的线程中使用。同样适用于从它们获取的任何对象)。

有关如何使用 Core Data 进行并发的更多信息,请参阅此 WWDC 会话

仅尝试从主线程使用您的核心数据堆栈(以及您从中获取的任何对象)。

于 2012-04-11T09:41:28.193 回答