我开始使用Core Spotlight来索引我的应用中的项目。正如我想的那样,应用程序崩溃在我的设备上,但不是模拟器,因为我有2000多个项目要索引。我注意到,当我向我的NSMutableArray
项目添加大约427个项目时,我开始收到内存警告,并在到达540个项目时完全崩溃。
我知道batch
这些索引的能力,我可以在他们的网站Apple - Index App Content - Using Advanced Features上看到Apple提供的代码。但是,我不确定如何实现这一点。
有人可以添加一些代码或指出我正确的方向来实现这个目标吗?
我试图索引的项目大约有2000多个名字,包括我公司的电话号码,电子邮件,职位和部门信息。我有一个内置的文件,但我也定期下载文件以获取最新信息。我将数据保存为plist
,而不是CoreData
。下载文件后,我首先删除当前索引的所有内容然后添加所有项目,然后启动索引。从服务器我无法确定数据是否已更改。
我的目标是一次索引大约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!");
}
}];
}
}];
}
}
}
}
}
谢谢!
答案 0 :(得分:0)
所以你的内存问题实际上与核心聚光灯无关,因为你的代码在你到达之前就已经崩溃了。您应该剖析以确切地检查所有内存是什么,我猜您生成的图像是其中很大一部分,特别是因为您使用的是PNG数据而不是JPEG(考虑到图像的简单性质,具有显着的压缩) 。当您真正想要的是图像时,创建图像视图似乎也非常浪费,并且在循环的每次迭代中创建新的图像视图都是浪费的...
所以,你不应该真正批处理核心聚光灯处理,你应该批处理你的文件处理。你可以通过多种方式做到这一点,但重点是你要将文件中的100个项目处理成一个数组,然后将其传递给核心聚光灯并等待它完成。然后你开始文件中的下100个项目。
您可以使用异步NSOperation
执行此操作。你可以使用递归块和GCD来完成它。