本文介绍了CoreSpotlight索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试在我的应用中实现CoreSpotlight。

Hi I'm trying to implement CoreSpotlight in my app.

我需要每次运行此索引,或者只需运行一次就可以了是第一次安装?
如果应用程序被删除,我是否需要再次索引?

When indexing do I need to run this every time or is it sufficient to run this once when app is installed for the first time?If app is deleted do I need to index again?

这是我正在使用的代码:

Here's the code I'm using:

- (void)spotLightIndexing {

    NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"aDetailed" ofType:@"plist"];

    NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSArray *plistArray = [plistDict allKeys];

    for (id key in plistDict) {

        CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];

        // Set properties that describe attributes of the item such as title, description, and image.

        attributeSet.title = key;
        attributeSet.contentDescription = [plistDict objectForKey:key];

//*************************************

 attributeSet.keywords = plistArray; // Another Q: do i need this????

//**************************************  

        // Create an attribute set for an item

        UIImage *image = [UIImage imageNamed:@"icon.png"];
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
        attributeSet.thumbnailData = imageData;

        // Create a searchable item, specifying its ID, associated domain, and the attribute set you created earlier.

        CSSearchableItem *item;
        NSString *identifier = [NSString stringWithFormat:@"%@",attributeSet.title];

        item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:@"com.example.apple_sample.theapp.search" attributeSet:attributeSet];

        // Index the item.

        [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
                        if (!error)
           NSLog(@"Search item indexed");
                       else {
                            NSLog(@"******************* E R R O R *********************");


        }];

    }
}

谢谢

推荐答案

按指定索引。因此,如果你在 didFinishLaunchingWithOptions 中放置 spotLightIndexing 方法,它会在每次启动时自然地索引项目,除非你设置一个bool课程。如果应用程序被删除,它将再次重新索引,因为NSUserDefault值将被清零。这就是为什么他们通过批量更新或其他方法提供添加/更改/更新索引的原因

Its indexed as specified. So if you put your spotLightIndexing method in didFinishLaunchingWithOptions it will of naturally index items every launch, unless you set a bool of course. If the app is deleted it will re-index again as the NSUserDefault values will be zeroed out. That is why they offer you add/altering/updating indices via batch updates or other methods as annotated here

因为你从本地plist填充它与网络相反,您必须自己进行更新或创建索引维护应用扩展程序。

Since your populating it from a local plist as opposed to the web, you will have to do the updates yourself or create an index-maintenance app extension.

如果您观看有关此主题的WWDC视频,您将看到使用域标识符,组可以轻松更新或删除域。 这是一个很好的手表。

If you watch the WWDC video on this topic, you will see that's easy to update or delete domains by a 'group' using the domain identifier. Source It's a good watch.

就关键字而言,在文档完全支持iOS9 API之前,没有任何意义。但只是通过阅读Apple在此公开提供的内容是您应该考虑的注意事项:

As far as the keywords, there is no telling until the documents are fully supporting iOS9 APIs. But just by reading what Apple has publicly provided here is a note you should consider :

位于新的搜索功能摘要之后。然后它继续说明原因:

That is located after the new Search features summary. And it goes on to say why:

换句话说,假设您按照他们的意图合并 NSUserActivity ,因为它可以应用于应用的所有用户,而不是只是进行查询的人,它可以在同一个搜索中多次填充。因此,根据Apples的建议,尽量不要使用关键字,除非您确定,特别是根据您的示例,关键字has = uniqueIdentifier。

So in other words, say you incorporate NSUserActivity as they intend you to because it can apply to all users of your app, not just the person doing the querying, it can populate multiple times in the same search. So, based on Apples suggestions, try not to use keywords unless your sure, especially based off your example, where the keyword already = uniqueIdentifier.

就我个人而言,我已经在我的应用程序中实现了它并且喜欢它,但是,我使用网络标记,几乎立即进行批量更新,而不是你的路线,你必须实际推出新的更新,以重新更新/删除索引。

Personally, i've already implemented this into my app and love it, however, I use web mark-up which makes batch updates almost instantaneously, as opposed to your route, where you would have to actually push out a new update to re-update/delete the indices.

这篇关于CoreSpotlight索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:47