mercredi 22 juin 2016

How to Properly Batch Index in Core Spotlight


I am starting to use Core Spotlight to index items in my app. As I figured it would, the app crashes on my device, but not the simulator, because I have 2000+ items to index. I noticed I start getting memory warning by the time I add about 427 items to my NSMutableArray holding my items, and crashes completely by the time it gets to 540 items.

I am aware of the ability to batch these indexes, and I can see the code presented by Apple on their site Apple - Index App Content - Using Advanced Features. However, I'm not sure exactly how to implement this.

Could someone add some code or point me in the right direction to accomplishing this?

Update #1 (2016/06/21)

The items I'm trying to index are about 2000+ names, with phone numbers, emails, title, and department information for my company. I have a file built in but I also download a file periodically to get up-to-date info. I save the data as a plist, not into CoreData. Once the file downloads I initiate the index, by first deleting everything currently indexed and then adding all the items. From the server I cannot determine if data has changed.

My goal is to index about 200-400 items at a time until all items have been indexed. Below is how I'm currently indexing without batching.

- (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!");
                                    }
                                }];
                            }
                        }];
                    }
                }
            }
        }
    }

Thanks!


Aucun commentaire:

Enregistrer un commentaire