3

以前の投稿で述語を作成しようとしましたが、3時間後には機能させることができなかったため、この方法で作成することになりました。

より効果的な方法が必要だと感じています。また、述語で大文字と小文字を区別しないようにする必要があります。

10,000台の車タイヤ、ホイール、シートを3つの車の部品として持っています。タイヤを持っているすべての車を見つけたいです。次に、ホイールが付いているすべての車を見つけたいと思います。次に、座席のあるすべての車を見つけたいと思います。(私は多くが重複することを知っていますが、それが私が必要としているものです)

もっと効果的な方法があれば教えてください。また、述語の大文字と小文字を区別しないようにする方法を教えてください。

前もって感謝します!

-(NSArray*) loadCarsFromCoreData:(NSMutableArray*)inputCarParts{

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

    //To find the cars we are using the car parts
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarParts" inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];
    NSError *error;
    NSMutableArray *predicates =[self parseCarPartsIntoAPredicateArray:inputCarParts];
    for (int i=0; i<[predicates count]; i++) {
        [fetchRequest setPredicate:[predicates objectAtIndex:i]];

        NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
        NSLog(@"results = %i",[records count]);
        [totalResults addObjectsFromArray:records];
    }
    NSLog(@"results = %i",[totalResults count]);
    return [NSArray arrayWithArray:totalResults];
}

-(NSMutableArray*)parseCarPartsIntoAPredicateArray:(NSMutableArray*)inputCarParts{
    NSMutableArray *returnArray=[[NSMutableArray alloc]init];
    for (int i=0; i<[inputCarParts count]; i++) {
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"partName == %@",[inputCarParts objectAtIndex:i]];
        [returnArray addObject:predicate];
    }

    return returnArray;
}
4

2 に答える 2

11

It sounds like what you're really looking for is how to construct a predicate based on an array of possible matches. In that case you'd do something like this:

NSMutableArray *partPredicates = [NSMutableArray arrayWithCapacity:[inputCarParts count]];
for (NSString *partName in inputCarParts) {
    NSPredicate *currentPartPredicate = [NSPredicate predicateWithFormat:@"partName =[c] %@", partName];
    [partPredicates addObject:currentPartPredicate];
}
NSPredicate *fullPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:partPredicates];

The resulting predicate would then be something like partName ==[c] "tire" OR partName ==[c] "wheel" OR partName ==[c] "seat", with one component per array entry. Do one fetch to evaluate them all in one shot.

This will not get duplicate results if you use it in the fetch. You left a comment on the question that indicates you don't want duplicates. If that's the case though, then I'm not sure what I know many will be duplicates, but that's what I need means. It sounds like you wanted the dupes, but you said you didn't.

于 2013-01-02T18:49:15.280 に答える
3

複製が必要な場合は、現在行っているように3つの個別のクエリを実行する必要があると思います。

If the duplicates aren't important, you should try creating a compound predicate by using OR between the three predicates you need. The following will find all the cars that either have the specified wheel, tire, or seat:

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"wheelPartName == %@ OR tirePartName == %@ OR seatPartName == %@", wheelPart, tirePart, seatPart];

To make your searches case insensitive, use ==[c] instead of ==, so the predicate in your code would look like:

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"partName ==[c] %@",[inputCarParts objectAtIndex:i]];

Take a look at this reference: String Predicate Programming.

于 2013-01-02T18:09:26.860 に答える