Skip to content

Commit

Permalink
[restguide] fix indexing to prevent race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
conceptdev committed Feb 3, 2016
1 parent 39bc809 commit d2e8e7c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
3 changes: 2 additions & 1 deletion RestaurantGuide/iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public override bool FinishedLaunching (UIApplication app, NSDictionary options)

LoadApplication (new App ());

// for debugging Properties
// for debugging Application.Properties
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
Console.WriteLine (documents);
// then look in /.config/.isolated-storage/PropertyStore.forms

// Index content for CoreSpotlight search!
if (UIDevice.CurrentDevice.CheckSystemVersion (9, 0)) {
// Code that requires iOS 9, like CoreSpotlight or 3D Touch
SearchModel = new SpotlightHelper (restaurants);
Expand Down
46 changes: 28 additions & 18 deletions RestaurantGuide/iOS/SpotlightHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace RestaurantGuide.iOS
{
public class SpotlightHelper
{
/// <summary>Find a restaurant by a unique identifier</summary>
/// <returns>Restaurant Name</returns>
public string Lookup (string num) {
var res = from r in restaurants
Expand All @@ -20,13 +21,20 @@ public string Lookup (string num) {
return Random (); // HACK: deal with bad data from NSUserActivity (or CoreSpotlight)
}
}
/// <summary>
/// Select a random restaurant, used for the '
/// </summary>
public string Random () {
var r = new Random ();
var rn = r.Next (0, restaurants.Count - 1);
Console.WriteLine ($"Random number {rn} in {restaurants.Count}");
return restaurants[rn].Name;
}
List<Restaurant> restaurants;

/// <summary>
/// Add the restaurant collection to the CoreSpotlight index
/// </summary>
public SpotlightHelper (List<Restaurant> restaurants)
{
this.restaurants = restaurants;
Expand All @@ -35,30 +43,32 @@ public SpotlightHelper (List<Restaurant> restaurants)
var attributeSet = new CSSearchableItemAttributeSet (UTType.Text);
attributeSet.Title = r.Name;
attributeSet.ContentDescription = r.Cuisine;
attributeSet.TextContent = r.Chef;
attributeSet.TextContent = r.Chef; // also allow search by chef's name

var dataItem = new CSSearchableItem (r.Number.ToString(), "com.xamarin.restguide", attributeSet);
dataItems.Add (dataItem);
}

// HACK: index should be 'managed' rather than deleted/created each time - keep track of what's indexed?
// HACK: index could be 'managed' rather than deleted/created each time - keep track of what's indexed?
// see the "To9o" sample for a better user-input search indexing strategy
CSSearchableIndex.DefaultSearchableIndex.DeleteAll(null);

CSSearchableIndex.DefaultSearchableIndex.Index (dataItems.ToArray<CSSearchableItem> (), err => {
if (err != null) {
Console.WriteLine (err);
Xamarin.Insights.Report(new Exception("CoreSpotlight Index Failed"), new Dictionary <string, string> {
{"Message", err.ToString()}
}, Xamarin.Insights.Severity.Error);
} else {
Console.WriteLine ("Indexed items successfully");
Xamarin.Insights.Track("CoreSpotlight", new Dictionary<string, string> {
{"Type", "Indexed successfully"}
});
}
CSSearchableIndex.DefaultSearchableIndex.DeleteAll(deletErr =>
{
// Start the new index only after the old one is completely deleted;
// important for larger indexes, where DeleteAll might take time to finish (thanks @jamesmontemagno)
CSSearchableIndex.DefaultSearchableIndex.Index (dataItems.ToArray<CSSearchableItem> (), err => {
if (err != null) {
Console.WriteLine ("Index failed " + err);
Xamarin.Insights.Report(new Exception("CoreSpotlight Index Failed"), new Dictionary <string, string> {
{"Message", err.ToString()}
}, Xamarin.Insights.Severity.Error);
} else {
Console.WriteLine ("Indexed items successfully");
Xamarin.Insights.Track("CoreSpotlight", new Dictionary<string, string> {
{"Type", "Indexed successfully"}
});
}
});
});
}
}
}

}

0 comments on commit d2e8e7c

Please sign in to comment.