简体   繁体   中英

Xamarin Android - How to improve the speed the reliability of the program

So I'm doing a program that interacts with Firebase Firestore, I'm kind a new to xamarin android so I'm not sure yet how to get work fine.

So I have some problems with the program the most important is:

  1. I have a Activity with fragments with snapshot listeners that each time I change the fragment the listener restarts and get the data again to the recycle view Ex:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
      var view = inflater.Inflate(Resource.Layout.fragment_service, container, false);
      recyclerview = view.FindViewById<RecyclerView>(Resource.Id.fragment_service_RecyclerView);
      recyclerview.SetLayoutManager(new LinearLayoutManager(recyclerview.Context));
      ListenForChanges();
      return view;
    }

ListenForChanges() ->

      CrossCloudFirestore.Current
     .Instance
     .GetCollection("FichaServicos")
     .WhereEqualsTo("idFicha", Doc.id)
     .AddSnapshotListener((snapshot, error) =>
     {
       if (snapshot != null)
       {
         foreach (var documentChange in snapshot.DocumentChanges)
         {
           switch (documentChange.Type)
           {
    ...

So my question is, can I somehow save the data globally or send the data between fragments so I don't need to request the data every time from firestore?

save the data globally

Actually , there are many solutions which can implement it

Option 1 :

You could use SharedPreferences . For example, set data:

        var data = GetSharedPreferences("Data", 0);
        var editor = data.Edit();
        editor.PutString("name","ABC");
        editor.Commit();

And get data:

        var data = GetSharedPreferences("Data", 0);
        string name = data.GetString("name", "default");

Option 2:

If the type of data is customize (like custom model or list), you could define a Singleton Class for Data Manager

public class DataManager
{
    private static DataManager instance;


    public List<string> Source { get; set; }

    //other data that ylou want to save , you need to defien them in advance
    //public ...

    private DataManager() { }

    public static DataManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new DataManager();
            }
            return instance;
        }
    }
}

And reference it like following

//set the value after you get it from filestore
DataManager dataManager = DataManager.Instance;
dataManager.Source = xxx;

Option 3 :

You could use local database like sqlite . Check the official docs for more details .

send the data between fragments

In the Activity

Bundle mybundle = new Bundle();
mybundle.PutString("MyDataTag", "Hello");
//you can also put other data like int , float and long
FragmentTransaction fragmentTransaction = SupportFragmentManager.BeginTransaction();
var myFragment = new VerifyReportFragment();
myFragment .Arguments = mybundle;

in the Fragment OnCreateView

String stringData= Arguments.GetString("MyDataTag");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM