void OnLoadNewPostComplted(CKQueryCursor cursor, NSError operationError, List <Post> newPosts, Post lastRecordInOperation, Post retryPost) { Error error = HandleError(operationError); switch (error) { case Error.Success: // lastRecordCreationDate is the most recent record we've seen on server, let's set our property to that for next time we get a push lastPostSeenOnServer = lastRecordInOperation ?? lastPostSeenOnServer; // This sorts the newPosts array in ascending order newPosts.Sort(PostComparison); // Takes our newPosts array and inserts the items into the table array one at a time foreach (Post p in newPosts) { updateCellArrayQueue.DispatchAsync(() => { PostCells.Insert(0, p); DispatchQueue.MainQueue.DispatchAsync(reloadHandler); }); } DispatchQueue.MainQueue.DispatchAsync(RefreshControl.EndRefreshing); break; case Error.Retry: Utils.Retry(() => LoadNewPosts(retryPost), operationError); break; case Error.Ignore: Console.WriteLine("Error: {0}", operationError.Description); DispatchQueue.MainQueue.DispatchAsync(RefreshControl.EndRefreshing); break; default: throw new NotImplementedException(); } }
void OnPostLoadingCompleted(CKQueryCursor cursor, NSError operationError) { Error error = HandleError(operationError); switch (error) { case Error.Success: postCursor = cursor; haveOldestPost = cursor == null; isLoadingBatch = false; PostCells.AddRange(downloadingBatchStorage); if (lastPostSeenOnServer == null && PostCells.Count > 0) { lastPostSeenOnServer = PostCells [0]; DispatchQueue.MainQueue.DispatchAsync(RefreshControl.EndRefreshing); } DispatchQueue.MainQueue.DispatchAsync(reloadHandler); break; case Error.Retry: Utils.Retry(() => { isLoadingBatch = false; LoadBatch(); }, operationError); break; case Error.Ignore: isLoadingBatch = false; DispatchQueue.MainQueue.DispatchAsync(RefreshControl.EndRefreshing); Console.WriteLine("Error: {0}", operationError.Description); break; default: break; } }
public void ResetWithTagString(string tags) { // Reloads table with new tag settings // First, anything the table is updating with now is potentially invalid, cancel any current updates fetchRecordQueue.CancelAllOperations(); // This should only be filled with array add operations, best to just wait for it to finish updateCellArrayQueue.DispatchSync(() => { }); // Resets the table to be empty PostCells.Clear(); lastPostSeenOnServer = null; reloadHandler(); // Sets tag array and prepares table for initial update tagArray = string.IsNullOrWhiteSpace(tags) ? new string[0] : tags.ToLower().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); postCursor = null; isLoadingBatch = false; haveOldestPost = false; LoadBatch(); }
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); }
public void ResetWithTagString(string tags) { // Reloads table with new tag settings // First, anything the table is updating with now is potentially invalid, cancel any current updates fetchRecordQueue.CancelAllOperations (); // This should only be filled with array add operations, best to just wait for it to finish updateCellArrayQueue.DispatchSync (() => { }); // Resets the table to be empty PostCells.Clear (); lastPostSeenOnServer = null; reloadHandler (); // Sets tag array and prepares table for initial update tagArray = string.IsNullOrWhiteSpace (tags) ? new string[0] : tags.ToLower ().Split (new char[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries); postCursor = null; isLoadingBatch = false; haveOldestPost = false; LoadBatch (); }
void OnLoadNewPostComplted(CKQueryCursor cursor, NSError operationError, List<Post> newPosts, Post lastRecordInOperation, Post retryPost) { Error error = HandleError (operationError); switch (error) { case Error.Success: // lastRecordCreationDate is the most recent record we've seen on server, let's set our property to that for next time we get a push lastPostSeenOnServer = lastRecordInOperation ?? lastPostSeenOnServer; // This sorts the newPosts array in ascending order newPosts.Sort (PostComparison); // Takes our newPosts array and inserts the items into the table array one at a time foreach (Post p in newPosts) { updateCellArrayQueue.DispatchAsync (() => { PostCells.Insert (0, p); DispatchQueue.MainQueue.DispatchAsync (reloadHandler); }); } DispatchQueue.MainQueue.DispatchAsync (RefreshControl.EndRefreshing); break; case Error.Retry: Utils.Retry(()=> LoadNewPosts(retryPost), operationError); break; case Error.Ignore: Console.WriteLine ("Error: {0}", operationError.Description); DispatchQueue.MainQueue.DispatchAsync(RefreshControl.EndRefreshing); break; default: throw new NotImplementedException (); } }
void OnPostLoadingCompleted(CKQueryCursor cursor, NSError operationError) { Error error = HandleError (operationError); switch (error) { case Error.Success: postCursor = cursor; haveOldestPost = cursor == null; isLoadingBatch = false; PostCells.AddRange (downloadingBatchStorage); if (lastPostSeenOnServer == null && PostCells.Count > 0) { lastPostSeenOnServer = PostCells [0]; DispatchQueue.MainQueue.DispatchAsync(RefreshControl.EndRefreshing); } DispatchQueue.MainQueue.DispatchAsync (reloadHandler); break; case Error.Retry: Utils.Retry (() => { isLoadingBatch = false; LoadBatch (); }, operationError); break; case Error.Ignore: isLoadingBatch = false; DispatchQueue.MainQueue.DispatchAsync (RefreshControl.EndRefreshing); Console.WriteLine ("Error: {0}", operationError.Description); break; default: break; } }
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); }