Reputation: 954
I have singletone for current user (in my app is doctor):
sealed class CurrentDataStorage
{
private static CurrentDataStorage dataStorage;
private CurrentDataStorage()
{
}
public static CurrentDataStorage DataStorage
{
get
{
if(dataStorage == null)
{
dataStorage = new CurrentDataStorage();
}
return dataStorage;
}
}
public Doctor CurrentDoctor { get
{
return CurrentDoctor;
}
set
{
if(CurrentDoctor != value)
CurrentDoctor = value;
}
}
}
If i use this in viewmodel class i get crash my app. Maybe CurrentDoctor is null. I create some doctor in authorization page (it's stub for user create in backend):
public void OnAuthorization(object sender, EventArgs a)
{
CurrentDataStorage.DataStorage.CurrentDoctor = new Doctor("Иван", "Иванов", "Иванович", "[email protected]", "superdoctor321",
"Princeton Plainsboro", Model.Enums.Position.Researcher, Model.Enums.Category.Highest, Model.Enums.AcademicDegree.Doctor);
Application.Current.MainPage = new ProfilePage();
}
ProfilePage is masterdetail. I create this panel with binding:
Page code look like this:
public ProfilePageMaster()
{
InitializeComponent();
this.BindingContext = new ProfilePageMasterViewModel();
}
class ProfilePageMasterViewModel : INotifyPropertyChanged
{
Doctor doctor;
public ProfilePageMasterViewModel()
{
doctor = CurrentDataStorage.DataStorage.CurrentDoctor;
}
public String FullName
{
get
{
return doctor.FullName;
}
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged == null)
return;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
What i do wrong in using singletone?
UPD: this crash report (It doesn't tell me anything)
Build fingerprint: 'Xiaomi/kenzo/kenzo:6.0.1/MMB29M/8.11.8:user/release-keys' Revision: '0' ABI: 'arm64' pid: 10207, tid: 10207, name: hecksorexamarin >>> com.pesiik.checksorexamarin <<< signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7fc77fad00 x0 0000007fc77fad00 x1 000000559713ab50 x2 0000000000000290 x3 0000007f97712000 x4 0000000000000000 x5 0000007f977126f0 x6 0000007fc77facf0 x7 0000007f79179f60 x8 0000007f79179f70 x9 0000007f79179f60 x10 0000007f92cc1840 x11 0000007f92ccd630 x12 0000007f92cb5748 x13 0000007f92ccaa70 x14 0000000000000003 x15 00000055977c7c58 x16 0000007f8e457df8 x17 0000007f9767bc08 x18 6000000000000000 x19 0000000000000000 x20 0000007f79072868 x21 0000007f79072868 x22 0000007f79117d90 x23 0000007f7907f2b0 x24 0000000000000000 x25 0000000000000000 x26 0000007f79179f70 x27 0000000012cbe640 x28 00000000729a2507 x29 0000007fc77fb020 x30 0000007f8e2344f8 sp 0000007fc77fad00 pc 0000007f9767bd54 pstate 0000000020000000
backtrace: #00 pc 0000000000019d54 /system/lib64/libc.so (memcpy+332) #01 pc 00000000001094f4 /data/app/Mono.Android.DebugRuntime-1/lib/arm64/libmonosgen-64bit-2.0.so
Upvotes: 1
Views: 60
Reputation: 11820
Most likely your error is not related with singleton. Your property is killer - it accessing itself:
public Doctor CurrentDoctor
{
get
{
return CurrentDoctor;
}
set
{
if(CurrentDoctor != value)
CurrentDoctor = value;
}
}
Use backing field instead
private Doctor _currentDoctor
public Doctor CurrentDoctor
{
get
{
return _currentDoctor;
}
set
{
if(_currentDoctor != value)
_currentDoctor = value;
}
}
Upvotes: 1