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(); } }
// Called when users pulls to refresh // This adds new items to the beginning of the table public void LoadNewPosts(Post post = null) { // If we don't have any posts on our table yet, // fetch the first batch instead (we make assumptions in this method that we have other posts to compare to) if (TryLoadFirstBatch()) { return; } // We want to strip all posts we have that haven't been seen on the server yet from tableview (order isn't guaranteed) var loc = PostCells.IndexOf(lastPostSeenOnServer); List <Post> newPosts = PostCells.Take(loc).ToList(); PostCells.RemoveRange(0, loc); // If we had a post passed in and it matches our tags, we should put that in the array too if (MatchesTags(post)) { newPosts.Add(post); } else { post = null; } // The last record we see will be the most recent we see on the server, we'll set the property to this in the completion handler Post lastRecordInOperation = null; var postQuery = CreateLoadNewPostQuery(); var queryOp = new CKQueryOperation(postQuery); queryOp.DesiredKeys = desiredKeys; queryOp.RecordFetched = (CKRecord record) => OnLoadNewPostFetchRecord(record, newPosts, ref lastRecordInOperation); queryOp.Completed = (CKQueryCursor cursor, NSError operationError) => OnLoadNewPostComplted(cursor, operationError, newPosts, lastPostSeenOnServer, post); queryOp.Database = PublicDB; fetchRecordQueue.AddOperation(queryOp); }
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(); }