简体   繁体   English

Xamarin Android - 如何提高程序的可靠性

[英]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.所以我正在做一个与 Firebase Firestore 交互的程序,我是 xamarin android 的新手,所以我不确定如何正常工作。

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() -> 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?所以我的问题是,我能否以某种方式全局保存数据或在片段之间发送数据,这样我就不需要每次都从 firestore 请求数据?

save the data globally全局保存数据

Actually , there are many solutions which can implement it实际上,有很多解决方案可以实现它

Option 1 :选项1 :

You could use SharedPreferences .您可以使用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:选项 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 :选项 3:

You could use local database like sqlite .您可以使用像 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在片段 OnCreateView 中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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