小编Tho*_*her的帖子

Android Room:如何同时从两个表中读取 - 重复的id列

我是Android Room的新手.我想从表中读取并从相关表中读取.这种关系非常普遍.一个表定义实例.另一个表定义了类型.想象一下Animal表和AnimalType表.几乎每次必须读取Animal表时,都需要读取AnimalType表.例如,我们想要显示动物名称(来自Animal表格)和猴子图标(来自AnimalType表格).

根据Android Room文档中的示例,这是对其进行建模的数据类:

public class AnimalWithType {
    @Embedded
    private Animal animal;

    @Embedded
    private AnimalType type;
    ...
Run Code Online (Sandbox Code Playgroud)

DAO可以使用单个查询查询数据.房间应该足够聪明,以填充两个子课程.

@Dao
public interface ZooDao
    @Query("select * from animal join animal_type on (animal.id = animal_type.id)")
    List<AnimalWithType> getAllAnimals();
Run Code Online (Sandbox Code Playgroud)

一如既往,理论很漂亮.现实被打破了.结果是以下错误:

Error:(8, 8) error: Multiple fields have the same columnName: id. Field names: animal > id, animalType > id.
Run Code Online (Sandbox Code Playgroud)

显然,Room对查询中只有一个"id"列以及它属于哪个类感到困惑.有两种可能的解决方案:1)我们可以为building_type.id创建一个列名别名,并告诉Room它.2)我们已经知道从Animal表到AnimalType表的外键(也许是animal_type_id)有另一个id列.

问题是我们如何与Room沟通.房间甚至能够处理这种情况吗?

关于如何解决这个问题的提示表示赞赏.我希望Room不需要做一些尴尬的事情就像给每个id列一个唯一的名字.

android android-room

14
推荐指数
2
解决办法
7424
查看次数

为什么 Android 布局的扩展需要这么长时间?

我在 Android 中有一个对话框。打开时有明显的延迟。这是我所做的调查:

  1. 使用 System.currentTime 的粗略计时,我的 onCreateDialog 方法在美好的一天需要 150 毫秒,在正常的一天需要 500-700 毫秒。

  2. 我实现了自己的 LayoutInflator.Factory。我的工厂什么都不做。它只是返回 null 让默认工厂完成工作。但它会写出自上次调用以来经过的时间。这样,我就可以打印出布局 xml 中的每个标签需要多长时间才能膨胀。非常令人惊讶的是,每个元素大约需要 20-70 毫秒才能膨胀!

  3. 我介绍了该应用程序。看来很多时间确实花在了视图的构造函数或LayoutParams上。

  4. 为了进行粗略检查,我测量了使用 System.currentTime 在 TextView 上调用构造函数的时间。事实证明,在功能强大的 Alienware PC 上的模拟器中,实例化 TextView 对象需要 20-70 毫秒!似乎有些不对劲,竟然要花这么长时间。

作为参考,布局膨胀、测量和渲染之间存在差异。我现在只关心通货膨胀的表现。

以下是我如何实现 LayoutInflator.Factory 来进行测量:

        LayoutInflater inflater = LayoutInflater.from(getActivity());
    LayoutInflater inflater2 = inflater.cloneInContext(getActivity());
    inflater2.setFactory(new LayoutInflater.Factory() {

        long inner = System.currentTimeMillis();

        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {
            Log.d(LOG_TAG, "onCreateView: Called factory for " + name + " took " + (System.currentTimeMillis() - inner));
            inner = …
Run Code Online (Sandbox Code Playgroud)

performance android android-layout

7
推荐指数
1
解决办法
2443
查看次数

Firestore +云功能:如何从另一个文档中读取

我正在尝试编写一个从另一个文档中读取的Google云功能.(其他文档=不是触发云功能的文档.)

这是一个寻找如何做这么简单的事情的宝藏.

  1. 云功能文档似乎建议查看管理SDK:"您可以通过DeltaDocumentSnapshot界面或通过Admin SDK进行Cloud Firestore更改."

    https://firebase.google.com/docs/functions/firestore-events

  2. Admin SDK建议编写以下代码行来获取客户端.但是哦,不,它不会解释客户.它将把我们送到文档中其他地方的疯狂追逐.

    var defaultFirestore = admin.firestore();

    "如果未提供应用程序,则默认的Firestore客户端或与提供的应用程序关联的Firestore客户端."

    https://firebase.google.com/docs/reference/admin/node/admin.firestore

  3. 该链接解析为一般概述页面,没有直接线索来确定下一步.

    https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/

  4. 挖掘一个大的,有一个很有前途的类叫做FireStoreClient.它有一个看似有希望的'getDocument'方法.参数似乎很复杂.它不是简单地将路径传递给方法,而是希望将整个文档/集合作为参数.

    https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/FirestoreClient#getDocument

    var formattedName = client.anyPathPath("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]"); client.getDocument({name: formattedName}).then(function(responses) { var response = responses[0]; // doThingsWith(response) })

所以,我正在尝试将所有这些信息组合到一个Google云功能中,该功能将从另一个文档中读取.

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.updateLikeCount4 = functions.firestore
    .document('likes/{likeId}').onWrite((event) => {
        return admin.firestore()
            .getDocument('ruleSets/1234')
            .then(function(responses) {
                 var response = responses[0];
                 console.log('Here is the other document: ' + response);
             })
    });
Run Code Online (Sandbox Code Playgroud)

这种方法失败了:

admin.firestore.getDocument is not a function
Run Code Online (Sandbox Code Playgroud)

我也试过了.admin.firestore.document,admin.firestore.doc,admin.firestore.collection等等.它们似乎都不是一个功能.

我想要的只是从我的Google云功能中读取另一个Firestore文档.

PS:他们说文件是你的朋友.这个文件是一个噩梦,遵循将所有线索分散到风的四个方向的原则!

javascript google-cloud-functions google-cloud-firestore

6
推荐指数
1
解决办法
7757
查看次数

Firestore +云功能:如何判断它是创建,更新还是删除事件

我想编写一个由Firestore写入事件触发的Firebase云功能.在函数内部,我必须知道写入是否实际上是创建,更新或删除功能.逻辑略有不同,但我不想复制和粘贴三个不同的事件处理程序.如何判断它是哪种事件类型?

云文档对我来说非常混乱.

事件的文档说,在FireStore的情况下,事件实际上是DeltaDocumentSnapshot.

https://firebase.google.com/docs/reference/functions/functions.Event

DeltaDocumentSnapshot的文档表明该类没有关于事件本身的信息(例如,创建,更新或删除的指示),但只有目标文档:

https://firebase.google.com/docs/reference/functions/functions.firestore.DeltaDocumentSnapshot

我找到了样本,作者似乎通过检查当前或以前的文档是否存在来推断创建/更新/删除.我试过了,但是我收到了一个错误.

https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js#L30

这是我尝试过的:

exports.updateLikeCount = functions.firestore
.document('likes/{likeId}').onWrite((event) => {
    if (event.data.data().exists()) {
      // Looks like the document still exists. It must be an update or create.
    }

    if (event.data.previous.data().exists()) {
      // Looks like the document existed before. It must be an update or delete.
    }
Run Code Online (Sandbox Code Playgroud)

该代码失败并出现此错误:

TypeError: event.data.exists is not a function
Run Code Online (Sandbox Code Playgroud)

google-cloud-functions google-cloud-firestore

5
推荐指数
2
解决办法
1672
查看次数

如何在 Android Studio 中分析测试?

我的 Android 应用程序有一些运行缓慢的功能。单元测试完美地捕捉到了这一点。单元测试执行显示它的运行速度比应有的速度慢得多。

Android Studio 不断在运行方法旁边提供一个菜单选项:“配置文件”而不是运行。我选择了该选项,但似乎没有发生与运行不同的情况。我希望 Android Studio 在测试完成后打开一个窗口,其中显示所有方法调用的时间。

在此输入图像描述

我搜索过 Google 和 Android 网站。我发现的所有内容都涉及 Android Studio 中的概要分析。

如何分析 Android 单元测试?(该配置文件选项的真正作用是什么?)

profiler profiling unit-testing android-studio

5
推荐指数
1
解决办法
3123
查看次数