I'm working on my app that will get data from online database. I would like to show progress bar. Problem is that I can not make it work.
Current situation is:
- I am using FreshMVVM.
- Method to get data form online is in class
FirebaseCloudNight
. There is static methodSynchronizeNights
with which I get all nights (with functionforeach
). I would like to show progress bar based on nights done/remaining to be synchronized. - In
ViewModel
I have propertyCurrentProgress
that is correctly bidden to View. I can not update this property correctly. - I left out a lot of code because I think is irrelevant. If you need more code please let me know.
- The only solution I found is to create new instance of MV class. But after additional reading I found out that, although I see that property changed, binding was not OK, because it changed new instance of property.
Issue is that the only way I found is to update this property based on new instance. But then MVVM does not work --> UI is not refreshed because it updates different instance of property. How can this be done?
Below is my code (if it will be of any help).
Code to retrieve DB from cloud
namespace iVanApp.Services {
public class FirebaseCloudNight : FirebaseCloudBaseHelper
{
static public async Task<bool> SynchronizeNights()
{
// Synchronize from Cloud to DB
foreach (var cloudItem in cloudNights)
{
CurrentProgress= currentNight / numberOfNights;
try
{
//Here I removed code for retriving data from online DB
currentNight ;
}
catch (Exception ex)
{
return false;
}
return true;
}
}
}
ViewModel Code
async Task GetData()
{
bool success = await FirebaseCloudNight.SynchronizeNights();>
}
float currentProgress;
public float CurrentProgress
{
get => currentProgress;
set
{
currentProgress = value;
RaisePropertyChanged();
}
}
EDIT: What I tried and does not work (because of obvious reasons)
namespace iVanApp.Services {
public class FirebaseCloudNight : FirebaseCloudBaseHelper
{
static public async Task<bool> SynchronizeNights()
{
// Synchronize from Cloud to DB
var VM = new UserAuthenticationPageModel();
foreach (var cloudItem in cloudNights)
{
VM.CurrentProgress = currentNight / numberOfNights;
try
{
//Here I removed code for retriving data from online DB
currentNight ;
}
catch (Exception ex)
{
return false;
}
return true;
}
}
}
CodePudding user response:
Found an answer that is working
Added code in ViewModel code behind
public class UserAuthenticationPageModel : FreshBasePageModel
{
public static UserAuthenticationPageModel Instance { get; private set; }
public UserAuthenticationPageModel()
{
Instance = this;
}
Modified code in FirebaseCloudNight
namespace iVanApp.Services { public class FirebaseCloudNight :
FirebaseCloudBaseHelper {
static public async Task<bool> SynchronizeNights()
{
// Synchronize from Cloud to DB
foreach (var cloudItem in cloudNights)
{
UserAuthenticationPageModel.Instance.CurrentProgress = currentNight / numberOfNights
try
{
//Here I removed code for retriving data from online DB
currentNight ;
}
catch (Exception ex)
{
return false;
}
return true;
}
} }