我不知道是否有可能與NSPredicate做到這一點:提前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.
我全新到這一點,所以也許這將幫助,我不知道該怎麼做。
你有沒有驗證,這其實是快? – 2013-03-20 17:51:12
@TomHarrington我真的不確定,但是我問過的每個地方都被告知使用整數比較而不是字符串比較。 – Mukhi 2013-03-20 17:53:43