如何在核心聚焦中正确批量索引

RoL*_*LLs 6 objective-c ios corespotlight

我开始使用Core Spotlight来索引我的应用中的项目.正如我想的那样,应用程序崩溃在我的设备上,但不是模拟器,因为我有2000多个项目要索引.我注意到,当我向我的物品添加大约427个物品时,我开始收到记忆警告NSMutableArray,并且当它到达540个物品时完全崩溃.

我知道batch这些索引的能力,我可以看到Apple在他们的网站上提供的代码Apple - 索引应用程序内容 - 使用高级功能.但是,我不确定如何实现这一点.

有人可以添加一些代码或指出我正确的方向来完成这个?

更新#1(2016/06/21)

我试图索引的项目大约有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!");
                                    }
                                }];
                            }
                        }];
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢!

Wai*_*ain 0

所以你的内存问题实际上与核心聚光灯无关,因为你的代码在你到达那里之前就崩溃了。您应该进行分析以准确检查占用所有内存的内容,我猜您正在生成的图像占了其中的很大一部分,特别是因为您使用的是 PNG 数据而不是 JPEG(鉴于图像的简单性质,具有显着的压缩) 。当您真正想要的是图像时,创建图像视图似乎也非常浪费,并且在循环的每次迭代中创建新的图像视图更是双重浪费......

因此,您不应该真正对核心聚光灯处理进行批处理,而应该对文件处理进行批处理。有多种方法可以做到这一点,但要点是,将文件中的 100 个项目处理到一个数组中,然后将其传递给核心聚光灯并等待它完成。然后开始文件中接下来的 100 个项目。

您可以使用异步来做到这一点NSOperation。您可以使用递归块和 GCD 来做到这一点。