如何在Xamarin-Android中从Firebase实时数据库中检索一条记录? [英] How to retrieve a single record from Firebase Real Time Database in Xamarin-Android?

查看:64
本文介绍了如何在Xamarin-Android中从Firebase实时数据库中检索一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要从Firebase Realtime数据库中读取一个实体,并将结果转换为与该结构匹配的C#类,但是我还没有找到明确的解决方案.有这棵树:

I need to read only one entity from Firebase Realtime Database and convert the result into a C# class matching the structure, but I haven't found a solution clear. Having this tree:

{
 "characters" : {
    "c1" : {
      "Email" : "peter.pan@example.com",
      "LastName" : "Pan",
      "Name" : "Peter"
    },
    "c2" : {
      "Email" : "harry.potter@example.com",
      "LastName" : "Potter",
      "Name" : "Harry"
    }
  }
}

这个C#类:

public class Character 
{
   public string Email {get; set; }
   public string Name { get; set; }
   public string LastName { get; set; }
}

我只想通过电子邮件检索一条记录,这一点我知道并且它是唯一的.但是,如果确实包含NuGet包 Xamarin.Firebase.Database 中不存在诸如OnceSingle<T>() or OnceSingleAsync<T>()之类的方法或扩展方法:

I would like to retrieve only one record by email, which I know and it is unique. But, if I do include the NuGet package Xamarin.Firebase.Database there not exists a method or extension method such as OnceSingle<T>() or OnceSingleAsync<T>() in it:

Character character = await FirebaseDatabase.Instance
  .GetReference(CharactersRoot)
  .OrderByChild("Email")
  .EqualTo("harry.potter@example.com") //Works until here
  .OnceSingleAsync<Character>(); //THIS METHOD IS NOT AVAILABLE. It doesn't work.

另一方面,通过包含软件包 Xamarin.Forms.FirebaseWrapper ,它是对以前 Firebase.Xamarin 的升级,它支持OnceSingleAsync<T>()像这样:

In the other hand, by including package Xamarin.Forms.FirebaseWrapper, an upgrade of the former Firebase.Xamarin, that supports OnceSingleAsync<T>() one can code something like this:

FirebaseClient fbClient= new FirebaseClient(Root);
var task = Task<Character>.Run(async () => 
            {
                return await fbClient.Child(CharactersRoot)
                    .OrderBy("Email")
                    .EqualTo("harry.potter@example.com")
                    .OnceSingleAsync<Character>().ConfigureAwait(false)
            });                 
            var character = task.Result;
            // do something with character...

但是它不起作用.引发 System.AggregateException:'发生一个或多个错误. (响应状态代码不表示成功:400(错误请求).)'

But it doesn't work. It raises a System.AggregateException: 'One or more errors occurred. (Response status code does not indicate success: 400 (Bad Request).)'

以下是我设法使它起作用的唯一方法,但它的性能不佳,因为它会检索所有集合并在本地执行搜索.

The following is the only I've managed to make it work, but it is under performant because it retrieves all the collection and performs the search locally.

    FirebaseClient fbClient= new FirebaseClient(Root);
    var task = Task<Character>.Run(async () =>
            {
                return await fbClient.Child(CharactersRoot)                                     
                                    .OnceAsync<Character>()
                                    .ConfigureAwait(false);
            });
            var character = task.Result
                            .Where(item => item.Object.Email == "harry.potter@example.com")
                            .Select(itm => itm.Object)
                            .FirstOrDefault();
            if (character != null)
            {
                //Do something with character
            }

任何能澄清我的想法使其变得更简单,更好的想法都会受到赞赏.

Any ideas that can help to clarify my mind to do it simpler and better will be appreciated.

推荐答案

public async Task<Character> GetAttendant(string email)
    {
        return (await firebase
          .Child("Character")
          .OnceAsync<Character>()).Select(item => new Character
          {
              EmailAddress = email,
          }).FirstOrDefault();
    }

//希望这会有所帮助

这篇关于如何在Xamarin-Android中从Firebase实时数据库中检索一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆