2012-12-07 2 views
0

fetchRequest를 필터링하려고합니다.Xcode - NSFetchRequest를 필터링하고 각 객체를 선택하십시오.

결과가 NSArray에로드되는 지점에 있습니다.

그러나 개별 항목을 꺼내기 위해 배열을 구문 분석해야합니다. 지금 당장은 하나의 객체처럼 보입니다.

나는이 지점에 도착하기 위해 사용하고 코드는 다음과 같이이 결과

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSManagedObjectContext *moc = coreDataController.mainThreadContext; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:moc]; 

[request setEntity:entity]; 


    // Order the events by name. 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 

    [request setSortDescriptors:@[sortDescriptor]]; 

    // Execute the fetch -- create a mutable copy of the result. 
    NSError *error = nil; 
    NSArray *categories = [[moc executeFetchRequest:request error:&error] mutableCopy]; 

    if (categories == nil) { 
     NSLog(@"bugger"); 
    } 

    NSObject *value = nil; 
    value = [categories valueForKeyPath:@"name"]; 

: 또한

value = (
) 

[DetailViewController loadPickerArray] 
[AppDelegate loadPickerArray] 
value = (
    "Cat Two", 
    "Cat Three", 
    "Cat One", 
    "Cat Four" 
) 

, 처음이 달려 있음을 유의하시기 바랍니다, 거기 없음 결과. 나는 그 시간의 약 50 %를 얻는다.

도움 주셔서 감사합니다.

+0

나는 당신이하려는 것을 이해하지 못합니다. '필터'란 특정 조건을 충족하는 반품을 의미합니까? 그렇다면 왜 당신은 당신의 요청에'NSPredicate'를 첨부하지 않으시겠습니까? –

+0

카테고리 엔티티에는 세 개의 필드가 있으며이 목적을 위해 이름 만 필요합니다. 나는 그것을 얻었지만 4 개가 아니라 하나의 객체 (괄호 안의)로 반환합니다. 나는 내가 뭘 잘못하고 있는지 몰라. –

+1

'categories'는 배열이고,'valueForKeyPath :'를 호출하면 배열에있는 각 객체의 값 (keypath)을 가진 배열을 반환합니다. –

답변

1

데이터를 필터링 할 수있는 몇 가지 방법이 있습니다.

선호되는 방법은 검색을 위해 술어를 사용하는 것입니다. 이것은 당신에게 최고의 성능을 줄 것입니다. 당신이 배열을 사용하여 필터링하려면

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSManagedObjectContext *moc = coreDataController.mainThreadContext; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:moc]; 

[request setEntity:entity]; 

// Order the events by name. 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[CD] %@", @"Cat"]; //This will return all objects that contain 'cat' in their name property. 

[request setPredicate:predicate]; 
[request setSortDescriptors:@[sortDescriptor]]; 

// Execute the fetch -- create a mutable copy of the result. 
NSError *error = nil; 
NSArray *categories = [moc executeFetchRequest:request error:&error]; 

if (categories == nil) { 
    NSLog(@"bugger"); 
} 

//Here you have the objects you want in categories. 

for(Category *category in categories) 
{ 
    NSLog(@"Category name: %@", category.name); 
} 

, 다음도 가능합니다 : 술어가 매우 강력 나는이의 Predicates Programming Guide을 읽어 보시기 바랍니다

NSMutableArray *categories = [[moc executeFetchRequest:request error:&error] mutableCopy]; 

[categories filterUsingPredicate:[NSPredicate predicateWithFormat:[NSPredicate predicateWithFormat:@"name CONTAINS[CD] %@", @"Cat"]] 

//Now, the only objects left in categories will be the ones with "cat" in their name property. 

, 그것은 훨씬 더 효율적으로 필터링하는 것입니다 결과는 상점에 저장됩니다.

+0

레오 감사합니다. 마지막 구절을 머리에 쓰지 못했습니다. 술어 가이드로 연결해 주셔서 감사합니다. 나는 이것들을 걸러 낼 필요가 없다. 그러나 이것은 매우 편리 할 것이다. –

+0

건배. 도움이 필요하면 알려주세요. –

+0

흠. 감사. 이 물건은 iCloud에서 가져옵니다. 그것은 시간의 약 50 %를 일하고 있습니다. 그래서 50 %, null 배열, 응용 프로그램을 다시 실행하고 배열 채워집니다 .. 아무것도 마음에와 서? 알림이 게시되었습니다 .. –