public async Task UpdatePostsAsync() { try { //Update from backend if possible and update database var newPosts = await JsonPlaceholderHelper.GetPostsAsync(); await App.Locator.PostDatabaseHelper.AddOrUpdatePostsAsync(newPosts); } catch (HttpRequestException e) { System.Diagnostics.Debug.WriteLine($"HTTP request exception {e.Message}"); } //Always dispay what's in the database var databasePosts = await App.Locator.PostDatabaseHelper.getPostsAsync(); //The following algorithm allows to replace only the items that changed in the observable collection for (int i = 0; i < databasePosts.Count; i += 1) { if (Posts.Count <= i) { Posts.Add(databasePosts[i]); } else if (databasePosts[i].Id != Posts[i].Id) { Posts[i] = databasePosts[i]; } } //delete remaining items in the Posts collection for (int i = databasePosts.Count; i < Posts.Count; i += 1) { Posts.RemoveAt(i); } }
public async Task UpdatePostsAsync() { var newPosts = await JsonPlaceholderHelper.GetPostsAsync(); this.Posts.Clear(); newPosts.ForEach((post) => { this.Posts.Add(post); }); }
public async Task UpdatePostsAsync() { var newPosts = await JsonPlaceholderHelper.GetPostsAsync(); this.Posts.Clear(); newPosts.ForEach((post) => { post.ImageUrl = "https://picsum.photos/70/?image=" + newPosts.IndexOf(post); this.Posts.Add(post); }); }