2012-04-11 4 views
-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 + 종류의 파일을 만들고 다음과 같은 코드가 있습니다.

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

     [email protected]"Antiquities"; 

     [email protected]"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]; 

       [email protected]"Antiques"; 

       [email protected]"Contains a Sub categories related to Antique pieces"; 




      }else { 
       category=[matches lastObject]; 
      } 






    return category; 

} 

사람이 관련 나를 도울 수 ..

내가 몇 일에서이에 붙어를 ...

당신의 -save:을 할 dispatch_async을 사용하고있는 것처럼 보이는
+0

정확히 무엇이 잘못 될지 잘 모르겠습니다. NSLog()는 executeFetchRequest로부터 오류를 내고 있습니까? –

+0

executeFetchRequest를 수행하는 동안 오류가 발생하지 않습니다. 응용 프로그램이 처음 실행될 때 이들 간의 관계를 보여줍니다. 하지만, 응용 프로그램을 종료하고 Category 엔티티를 가져 오면 관계 값이 존재하지 않습니다. 다음과 같은 내용을 보여줍니다. "" – prasad1250

답변

1

배경. 이것은 아마도 NSManagedObject을 다루는 "스레드 제한"규칙을 어기는 것입니다 (간단히 말해, 스레드가 작성된 스레드에서만 사용되어야합니다.이 스레드에서 가져온 모든 객체에도 동일하게 적용됩니다).

코어 데이터와 동시성을 수행하는 방법에 대한 자세한 내용은 this WWDC Session을 참조하십시오.

핵심 데이터 스택 (및 그로부터 가져 오는 모든 개체) 만 주 스레드에서 사용하도록하십시오.

+0

나는 그 dispatch_queue도 제거하려고했지만 ... 같은 일이 일어나고 있습니다 ... – prasad1250

관련 문제