我想知道是否可以使用 NSPredicate 执行此操作:
NSArray *letters = [[table.Letters componentsSeparatedByString:@","] mutableCopy];
NSArray *searched = [[searchString componentsSeparatedByString:@","] mutableCopy];
for (int i = 0; i<searched.count-1; i++)
{
if([letters[i] intValue] == [searched[i] intValue])
//return the record
else
break;
}
基本上我所做的是获取每个字符串,用逗号分隔,并创建一个整数数组(它们总是整数),然后将两者作为整数而不是字符串进行比较,以使其更快。
有什么办法可以做到这一点NSPredicate
吗?或者有什么方法可以用 CoreData 做到这一点?
显然这些都不重要,这是我的 Fetch 请求。
-(void)titles:(NSString *)toMatch
{
// THIS IS ALL SETTING UP THE STRING TO BE MATCHED AGAINST THE DATABASE
NSMutableArray *retval = [[NSMutableArray alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
lineIDArray = [[NSMutableArray alloc] init];
NSString *LabelText =toMatch;
NSString *compareString = @"";
for (int i = 0; i<LabelText.length; i++)
{
int letter = [LabelText characterAtIndex:i];
NSString *firstLetters;
if(letter<100)
{
firstLetters = [NSString stringWithFormat:@"0"];
firstLetters = [blah stringByAppendingFormat:@"%d", letter];
}
else
firstLetters = [NSString stringWithFormat:@"%d", letter];
compareString= [compareString stringByAppendingString:firstLetters];
compareString = [compareString stringByAppendingString:@","];
}
//ALL THE WAY UP TO HERE
//SETTING UP QUERY
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate;
//CHECKING WHICH TYPE OF SEARCH THE USER HAS SELECTED, MATCHING THE STRING FROM THE BEGINNING OR SEEING IF WHAT THEY SEARCHED ARE PRESENT IN THE FIRST LETTERS OF THE STRING
if ([defaults boolForKey:@"FLS"])
predicate = [NSPredicate predicateWithFormat:@"first_letter_start BEGINSWITH %@", compareString];
else
predicate = [NSPredicate predicateWithFormat:@"first_letter_start CONTAINS %@", compareString];
//SETTING UP REQUEST
[request setPredicate:predicate];
[request setReturnsObjectsAsFaults:NO];
[request setFetchBatchSize:100];
[request setEntity:entity];
[request setPropertiesToFetch:[NSArray arrayWithObjects:@"gurmukhi", @"shabad_id", nil]];
NSError *error;
//FETCHING
fetchedObjects = [managedObjectContext executeFetchRequest:request error:&error];
for (Gurbani *shabad in fetchedObjects)
{
[retval addObject:shabad.gurmukhi];
[lineIDArray addObject:[NSString stringWithFormat:@"%i", shabad.shabad_id]];
}
searchResults = retval;
[searchResultsTable reloadData];
};
这是模型:
这是我使用参数得到的输出-com.apple.CoreData.SQLDebug 1
:
2013-03-20 16:53:57.316 GurbaniKhoj[955:907] CoreData: sql: SELECT 0, t0.Z_PK FROM ZSHABAD t0 WHERE NSCoreDataStringSearch( t0.ZFIRST_LETTER_START, ?, 8, 0)
2013-03-20 16:53:59.982 GurbaniKhoj[955:907] CoreData: annotation: sql connection fetch time: 2.6653s
2013-03-20 16:53:59.983 GurbaniKhoj[955:907] CoreData: annotation: total fetch execution time: 2.6667s for 92 rows.
2013-03-20 16:53:59.988 GurbaniKhoj[955:907] CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, t0.ZGURMUKHI, t0.ZSHABAD_ID FROM ZSHABAD t0 WHERE t0.Z_PK IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) LIMIT 100
2013-03-20 16:54:00.058 GurbaniKhoj[955:907] CoreData: annotation: sql connection fetch time: 0.0692s
2013-03-20 16:54:00.059 GurbaniKhoj[955:907] CoreData: annotation: total fetch execution time: 0.0717s for 92 rows.
我对此很陌生,所以也许这会有所帮助,我不知道该怎么做。