I have a firebase data base and I do (CRUD) operation on it, so .. How can i check if there's a response or if there's data before Get it ? I retrive data but not check it .. How can I do it in try and catch ? Helper Class :
public class Helper
{
public static async Task<TEntity> Get<TEntity>(string url)
{
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
HttpClient client = new HttpClient(clientHandler);
var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TEntity>(json);
}
}
And this ViewModel class :
public class PatientListPageModelView : BaseViewModel
{
private ObservableCollection<Patient> _Patient = new ObservableCollection<Patient>();
public ObservableCollection<Patient> Patients { get => _Patient; set => SetProperty(ref _Patient, value, nameof(Patients)); }
private ICommand _Appearing;
private Patient _SelectedPatient;
public ICommand Appearing { get => _Appearing; set => SetProperty(ref _Appearing, value, nameof(Appearing)); }
public PatientListPageModelView()
{
Appearing = new AsyncCommand(async () => await LoadData());
}
async Task LoadData()
{
Patients = new ObservableCollection<Patient>((List<Patient>)await PatientService.GetUserPatients());
}
public Patient SelectedPatient
{
get => _SelectedPatient;
set
{
if (value != null)
{
App.Current.MainPage = new NavigationPage(new Views.TopBarProfile(value));
}
_SelectedPatient = value;
OnPropertyChanged();
}
}
}
So, where i can put check or handle this error ?
Edit : this is my Service to get the data :
public static async Task<IEnumerable<Patient>> GetUserPatients()
{
var url = await firebaseClient
.Child($"Specalists/406707265/Patients").BuildUrlAsync();
var result = await Helper.Get<List<Patient>>(url);
return result ;
}
>(url);` that might fail, then isn't the answer "wrap that line in a try-catch"??? More precisely: `try { var result = await Helper.Get
– ToolmakerSteve Apr 17 '22 at 19:33>(url); return result; } catch { ...your-exception-handling-code...; return ...; }`