2015-07-11 24 views
5

嗨我試圖在我的應用程序中實現CoreSpotlight。CoreSpotlight索引

索引編制時,我是否需要每次都運行它,或者是否足以在第一次安裝應用程序時運行一次? 如果應用程序被刪除,我需要再次索引?

下面是我使用的代碼:

- (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 *********************"); 


     }]; 

    } 
} 

謝謝

+0

你的代碼工程?因爲當我把這段代碼放到我的項目中時,編譯器不會給出'搜索項目索引'輸出! –

+0

@ Mc.Lover是的,它就像一個魅力!請參閱編輯答案以記錄任何錯誤 –

+0

請您看看這個問題嗎? http://stackoverflow.com/questions/33443833/set-title-property-from-nsarray-in-cssearchableitemattributeset,我用你的代碼,但沒有工作! –

回答

2

它作爲指定索引。所以如果你把你的spotLightIndexing方法放在didFinishLaunchingWithOptions裏,它會自然地在每次啓動時索引物品,除非你設置了一個布爾當然。如果應用程序被刪除,它將重新重新索引,因爲NSUserDefault值將被清零。這就是爲什麼他們爲您提供添加/變更/更新通過批量更新或者其他方法,指數下注釋here

由於您的,而不是在網絡本地的plist填充它,你將不得不自己動手更新或創建索引維護應用擴展。

如果您觀看關於此主題的WWDC視頻,您會發現使用域標識符可以輕鬆地通過「組」來更新或刪除域。 Source這是一個很好的手錶。

至於關鍵字,除非文檔完全支持iOS9 API,否則無法說明。但是,僅僅通過閱讀蘋果公司曾公開在這裏提供的是一個音符,你應該考慮:

重要提示:一定要避免過度索引你的應用內容或添加相關的關鍵字,並試圖改善的排名屬性您結果。由於iOS會測量用戶對搜索結果的參與程度,因此用戶找不到有用的項目會很快識別出來,最終可能會停止顯示在搜索結果中。

即位於新搜索功能摘要之後。它繼續說爲什麼:

當你結合多個搜索API,項目可以從多個地方索引。爲避免在搜索結果中爲用戶提供重複項目,您需要適當鏈接項目ID。爲了確保項目的ID鏈接,就可以在其他文字中使用的相同的值在搜索項的唯一標識符屬性,並在relatedUniqueIdentifier財產NSUserActivity對象的contentAttributes屬性中

所以,說你納入NSUserActivity,因爲他們打算讓你因爲它可以適用於所有您的應用的用戶,而不僅僅是查詢的人員,它可以在同一搜索中多次填充。因此,根據Apples的建議,儘量不要使用關鍵字,除非您確定,尤其是基於您的示例,其中關鍵字already = uniqueIdentifier。

就我個人而言,我已經在我的應用中實現了這一點,並且很喜歡它,但是,我使用網絡標記,這使得批量更新幾乎是瞬間的,而不是您的路線,您必須實際推出新的更新以重新更新/刪除索引。

+0

感謝您的洞察力。使用本地plist是我們做出的與網絡相反的決定,純粹是因爲如果plist發生變化,則無論如何都必須更新應用程序,以便合併新的條目/刪除。 –

+0

你能測試你的網絡標記正在被索引嗎?這在開發/調試中如何工作? –

+1

能否詳細說明批量索引 - (void)beginIndexBatch和endIndexBatchWithClientState:completionHandler? – 2015-08-20 14:03:46