async System.Threading.Tasks.Task ProcessTags(AsanaTask task) { var currentTags = SelectedTags.Select(x => x.id).ToList(); var tagsToAdd = new List <long>(); var tagsToRemove = new List <long>(); foreach (var tag in currentTags) { if (!OldTagsIds.Contains(tag)) { tagsToAdd.Add(tag); } } foreach (var tag in OldTagsIds) { if (!currentTags.Contains(tag)) { tagsToRemove.Add(tag); } } var taskTags = await new StorageService().GetTaskTagsByTask(task.id); var forInsert = new List <AsanaTagTask>(); var forDelete = new List <AsanaTagTask>(); foreach (var l in tagsToAdd) { var response = await new AsanaRespository().AddTag(task.id, l); if (AsanaClient.ProcessResponse(response, true)) { var link = new AsanaTagTask() { TagId = l, TaskId = task.id, id = Guid.NewGuid().GetHashCode() }; forInsert.Add(link); } } foreach (var l in tagsToRemove) { var response = await new AsanaRespository().RemoveTag(task.id, l); if (AsanaClient.ProcessResponse(response, true)) { var link = taskTags.FirstOrDefault(x => x.TagId == l); if (link != null) { forDelete.Add(link); } } } foreach (var asanaTagTask in forInsert) { await new StorageService().Insert(asanaTagTask); } foreach (var asanaTagTask in forDelete) { await new StorageService().Delete(asanaTagTask); } }