async partial void ButtonQuery_TouchUpInside(UIButton sender) { LabelResults.ResignFirstResponder(); ProgressBar.StartAnimating(); await helper.QueryBingImages(TextFieldSearch.Text); LabelResults.Text = "Search Results: " + helper.Images.Count; ImageCollection.ReloadData(); ProgressBar.StopAnimating(); }
void LoadImages() { // If we're already loading a set of images or there are no images left to load, just return lock (locker) { if (isLoadingBatch || firstThumbnailLoaded) { return; } else { isLoadingBatch = true; } } // If we have a cursor, continue where we left off, otherwise set up new query CKQueryOperation queryOp = null; if (imageCursor != null) { queryOp = new CKQueryOperation(imageCursor); } else { CKQuery thumbnailQuery = new CKQuery(Image.RecordType, NSPredicate.FromValue(true)); thumbnailQuery.SortDescriptors = Utils.CreateCreationDateDescriptor(ascending: false); queryOp = new CKQueryOperation(thumbnailQuery); } // We only want to download the thumbnails, not the full image queryOp.DesiredKeys = new string[] { Image.ThumbnailKey }; queryOp.ResultsLimit = UpdateBy; queryOp.RecordFetched = (CKRecord record) => { ImageCollection.AddImageFromRecord(record); InvokeOnMainThread(() => { loadingImages.StopAnimating(); ImageCollection.ReloadData(); }); }; queryOp.Completed = (CKQueryCursor cursor, NSError error) => { Error errorResponse = HandleError(error); if (errorResponse == Error.Success) { imageCursor = cursor; isLoadingBatch = false; if (cursor == null) { firstThumbnailLoaded = true; // If cursor is nil, lock this method indefinitely (all images have been loaded) } } else if (errorResponse == Error.Retry) { // If there's no specific number of seconds we're told to wait, default to 3 Utils.Retry(() => { // Resets so we can load images again and then goes to load isLoadingBatch = false; LoadImages(); }, error); } else if (errorResponse == Error.Ignore) { // If we get an ignore error they're not often recoverable. I'll leave loadImages locked indefinitely (this is up to the developer) Console.WriteLine("Error: {0}", error.Description); string errorTitle = "Error"; string dismissButton = "Okay"; string errorMessage = "We couldn't fetch one or more of the thumbnails"; UIAlertController alert = UIAlertController.Create(errorTitle, errorMessage, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create(dismissButton, UIAlertActionStyle.Cancel, null)); InvokeOnMainThread(() => PresentViewController(alert, true, null)); } }; PublicCloudDatabase.AddOperation(queryOp); }