Can't get batch indexing to work.

I'm having some difficulty getting items added to the search index using the item batching. With the same NSArray (items) of CSSearchableItem objects, the following code:


[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:items
     completionHandler: ^(NSError * __nullable error)
{
     if (error)
          NSLog(@"Error indexing items.");
     else
          NSLog(@"Items indexed.");
}
];


works fine and properly adds all the objects in the items array to the search index.


But substituting this code:


  
CSSearchableIndex *index = [CSSearchableIndex new];
[index beginIndexBatch];
[index indexSearchableItems:items completionHandler:nil];
[index endIndexBatchWithClientState:clientState completionHandler:^(NSError *error)
{
     NSLog(@"Item Batching: %@", error.description);
}
];


results in no errors in the log (error.description is null) but the items I tried to index do not show up in any searches using Spotlight.


I'm not sure what, if anything, I'm doing wrong. I'm fairly new to xcode/objective c/foundation so if there's anything that I should be doing that might seem obvious to most, let me know. Any help is appreciated and if anyone has actually managed to get the index batching to work, I'd love to hear about it.


- Dave W

Hi,


it work for me. Maybe you need to call fetchLastClientStateWithCompletionHandler first:

    CSSearchableIndex* index = [CSSearchableIndex new];
    [index fetchLastClientStateWithCompletionHandler:^(NSData * __nullable clientState, NSError * __nullable error)
     {
        if (error == nil)
        {
            [index beginIndexBatch];
         
            NSArray* items = ... ;

            [index indexSearchableItems:items
                      completionHandler:^(NSError * __nullable error)
             {
                 NSLog(@"indexSearchableItems (error %@)", error);
             }];

            NSData* clientState = [NSKeyedArchiver archivedDataWithRootObject:latestDate];
            [index endIndexBatchWithClientState:clientState completionHandler:^(NSError * __nullable error)
             {
                 NSLog(@"endIndexBatchWithClientState (error %@)", error);
             }]
        }
     }];


Dirk

Thanks for the answer. This lead me in the right direction. It wasn't specifically fetchLastClientStateWithCompletionHandler that I needed, but I had to make sure that the clientState I was sending to endIndexBatchWithClientState wasn't an empty NSData object. I think I had been using a freshly initialized NSData object so I think that was the problem.

Can't get batch indexing to work.
 
 
Q