아래 코드의 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
}
'insertNewObjectForEntityForName : InManagedObjectContext : '라고 부를 때마다 새로운 객체를 만들고 추가하는 중 ... – Wain
달성하려는 것은 무엇입니까? –
@Wain - 감사합니다. wain ... 같은 답변을 해주실 수 있습니까? 이 질문에 대한 답변으로 표시하고 싶습니다. –