[英]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:所以我的程序有一些问题,最重要的是:
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实际上,有很多解决方案可以实现它
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");
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;
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.