2014-01-21 3 views
0

아래 코드의 NSFetchRequest가 데이터의 배수 카운트에서 페치 중입니다. 예 : 3 번으로 배열 수를 계산하고 다음 번에 6 번으로 다음 번에 호출합니다 9 등. 그러나 데이터베이스에서 개수는 3으로 유지됩니다. 왜 이런 일이 발생합니까?코어 데이터 - 데이터의 실제 카운트를 두 번 가져 오는 NSFetchrequest

// the method is called in viewDidLoad 

- (void) create { 

[orderList removeAllObjects]; 
    NSError * error; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"OrderList" 
              inManagedObjectContext:context]; 
for (int addOrd = 0; addOrd < orderMainArray.count; addOrd++) { 

    GetOrders *getOrd = (GetOrders*)[NSEntityDescription insertNewObjectForEntityForName:@"GetOrders" inManagedObjectContext:context]; 
    OrderList *ordList = (OrderList*)[NSEntityDescription insertNewObjectForEntityForName:@"OrderList" inManagedObjectContext:context]; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 



    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"orderId contains[cd] %@", [[orderMainArray objectAtIndex:addOrd] objectForKey:@"orderId"]]; 
    [fetchRequest setPredicate:predicate]; 
    [fetchRequest setEntity:entity]; 

     NSLog(@"\n \n \n count for same data = %d", [context countForFetchRequest:fetchRequest error:&error]); 

    if ([context countForFetchRequest:fetchRequest error:&error] ==0) { 

     // all data is saved here 

     [getOrd addOrderListObject:ordList]; 
     if ([context save:&error]) { 
      NSLog(@"The save was successful! %@", ordList.orderId); 

     } 
     else { 
      NSLog(@"The save wasn't successful: %@", [error userInfo]); 
     } 
     } 
    } 
    NSFetchRequest *fetchRequest1 = [[NSFetchRequest alloc] init]; 
    [fetchRequest1 setEntity:entity]; 

    orderList = [[context executeFetchRequest:fetchRequest1 error:&error] mutableCopy]; // array count increases every time the method is called 


} 
+1

'insertNewObjectForEntityForName : InManagedObjectContext : '라고 부를 때마다 새로운 객체를 만들고 추가하는 중 ... – Wain

+0

달성하려는 것은 무엇입니까? –

+0

@Wain - 감사합니다. wain ... 같은 답변을 해주실 수 있습니까? 이 질문에 대한 답변으로 표시하고 싶습니다. –

답변

0

insertNewObjectForEntityForName:InManagedObjectContext:을 호출 할 때마다 새 개체를 만들고 추가하고있었습니다. 그래서 그것을 변경하고 지금은 잘 작동합니다.

0

코드는 가져 오기 요청과 새로운 관리 대상 개체의 삽입을 혼합합니다. 하나의 OrderList NSManagedObject 및 하나의 GetOrder NSManagedObject를 for 루프의 각 반복에 삽입합니다. 설명을 주면 orderMainArray.count 값은 3이라고 가정합니다.

관련 문제