Core Spotlightを使用して、アプリケーション内のアイテムのインデックスを作成し始めました。私はそれがわかったので、私のデバイス上でアプリがクラッシュしますが、シミュレータではありません。インデックスには2000以上のアイテムがあるからです。私は私のアイテムを保持している私のNSMutableArray
に約427個のアイテムを追加し、540アイテムに達するまでに完全にクラッシュするまでに私はメモリの警告を受け取ることに気付きました。Core Spotlightでインデックスを正しくバッチする方法
私はこれらのインデックスをbatch
に認識しており、アップルのサイトApple - Index App Content - Using Advanced Featuresに記載されているコードを見ることができます。しかし、私はこれを実装する方法を正確にはわかりません。
誰かがコードを追加したり、これを達成するための正しい方向に向けることができますか?
更新#1(2016年6月21日)
私はインデックスにしようとしているアイテムは、私の会社の電話番号、電子メール、タイトル、および部門情報を用いて約2000+名、です。私はファイルを内蔵していますが、ファイルを定期的にダウンロードして最新の情報を入手しています。私はCoreData
にではなく、plist
としてデータを保存します。ファイルがダウンロードされると、最初に索引付けされたすべてのものを削除し、次にすべてのアイテムを追加することによって、索引を開始します。サーバーからは、データが変更されたかどうかは判断できません。
私の目標は、すべてのアイテムのインデックスが作成されるまで、一度に約200〜400アイテムのインデックスを作成することです。以下は現在バッチ処理なしでインデックスを作成しているところです。
- (void)setupCoreSpotlightSearchFor:(NSString *)fileName {
if ([fileName isEqual:kPlistFileNameEmployee]) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *downloadFileUrl = kDirectoryEmployeeListDownload;
if ([fileManager fileExistsAtPath:[downloadFileUrl path]]) {
NSArray *dataArray = nil;
NSError *error = nil;
dataArray = [NSArray arrayPropertyListWithURL:downloadFileUrl error:&error];
if (!error && dataArray) {
NSMutableArray *items = nil;
int counter = 0;
for (NSDictionary *person in dataArray) {
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *idNum = [f numberFromString:[[person objectForKey:@"Id"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSString *firstName = [[person objectForKey:@"FirstName"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *lastName = [[person objectForKey:@"LastName"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *email = [[person objectForKey:@"Email"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *phone = [[person objectForKey:@"Extension"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *title = [[person objectForKey:@"Title"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *organization = [[person objectForKey:@"Organization"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *department = [[person objectForKey:@"Department"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kCIAttributeTypeImage];
Person *person = [Person createPersonWithId:idNum
FirstName:[NSString stringOrNil:firstName]
LastName:[NSString stringOrNil:lastName]
Email:[NSString stringOrNil:email]
PhoneNumber:[NSString stringOrNil:phone]
Title:[NSString stringOrNil:title]
Organization:[NSString stringOrNil:organization]
Department:[NSString stringOrNil:department]
];
if ([NSString stringOrNil:[person fullName]] != nil) {
[attributeSet setTitle:[person fullName]];
[attributeSet setContentDescription:[person title]];
NSMutableArray *keywords = [[NSMutableArray alloc] init];
if ([NSString stringOrNil:[person firstName]] != nil) {
[keywords addObject:[person firstName]];
}
if ([NSString stringOrNil:[person lastName]] != nil) {
[keywords addObject:[person lastName]];
}
if ([NSString stringOrNil:[person title]] != nil) {
[keywords addObject:[person title]];
}
if ([NSString stringOrNil:[person department]] != nil) {
[keywords addObject:[person department]];
}
if ([NSString stringOrNil:[person phoneNumber]] != nil) {
[keywords addObject:[person phoneNumber]];
}
if ([NSString stringOrNil:[person email]] != nil) {
[keywords addObject:[person email]];
}
[attributeSet setKeywords:keywords];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 180, 180)];
[imageView setImageWithString:[person initialsWithCapitalization:InitialsCapitalizationTypeBoth andLastNameFirst:NO] color:[UIColor colorWithRed:0.77 green:0.07 blue:0.19 alpha:1.0] circular:YES];
[attributeSet setThumbnailData:UIImagePNGRepresentation([imageView image])];
if ([NSString stringOrNil:[person phoneNumber]] != nil) {
[attributeSet setSupportsPhoneCall:[NSNumber numberWithInt:1]];
[attributeSet setPhoneNumbers:@[[person phoneNumber]]];
}
if ([NSString stringOrNil:[person email]] != nil) {
[attributeSet setEmailAddresses:@[[person email]]];
}
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%@", [person id]] domainIdentifier:@"com.myapp.index.employee" attributeSet:attributeSet];
if (item) {
if (!items) {
items = [[NSMutableArray alloc] init];
}
[items addObject:item];
counter++;
}
}
}
if (items) {
// delete current items
[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {
if (error) {
DLog(@"%@",error);
} else {
DLog(@"Deleted Index!");
// add new items
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:items completionHandler:^(NSError * _Nullable error) {
if (error) {
DLog(@"%@",error);
} else {
DLog(@"Added Index!");
}
}];
}
}];
}
}
}
}
}
ありがとうございます!
あなたは何を求めているのか分かりません...あなたの索引を浴びたいと思いますが、Appleが実装したコードを見ることはできますが、まだ混乱していますか?ここからは、https://www.toptal.com/ios/ios-9-spotlight-search-for-developers – KFDoom
@KFDoomで始まるリンクがあります。私は、私が提供したリンクに記載されているようにそれらをバッチしようとしています。 – RoLYroLLs
アプリが実行されていない間にバッチ更新をしたいですか?アップルのドキュメンテーションの上級セクションで、ほとんどあなたがリンクしていることがほとんどです。 – KFDoom