安卓编辑文本界面在调用Firebase后冻结了

0 人关注

我目前正在开发一个聊天应用程序,我遇到了一个EditText的问题,我从它那里获得用户的输入并将其发送到数据库。下面是一个与实际代码相似的测试,但仍会导致EditText UI滞后。

send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = chat.getText().toString();
            Message message = new Message(String.valueOf(new Random().nextInt(1000)),user,text,new Date());
            sendMessage(message);
            chat.getText().clear();
private void sendMessage(Message message){
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mDatabaseRef = mFirebaseDatabase.getReference();
    mDatabaseRef.child("messagesTest").child("chat")
            .push()
            .setValue(message);

当我删除sendMessage(message)方法时,编辑文本并不滞后,即我可以多次输入文本,但当我试图将其发送到数据库时,第一次工作正常,第二次尝试时,编辑文本用户界面滞后。

以下是我的日志(就我所见,没有错误)。

I/oject.beautyap: Background concurrent copying GC freed 281924(6MB) AllocSpace objects, 0(0B) LOS objects, 35% free, 10MB/16MB, paused 1.806ms total 157.154ms
W/ClassMapper: No setter/field for saved_photos found on class com.project.beautyapp.models.UserAccountSettings
W/ClassMapper: No setter/field for saved_photos found on class com.project.beautyapp.models.UserAccountSettings
I/oject.beautyap: Background concurrent copying GC freed 144593(4MB) AllocSpace objects, 0(0B) LOS objects, 21% free, 21MB/27MB, paused 2.984ms total 380.004ms
I/oject.beautyap: NativeAlloc concurrent copying GC freed 541417(11MB) AllocSpace objects, 0(0B) LOS objects, 22% free, 20MB/26MB, paused 825us total 340.997ms
I/oject.beautyap: Background concurrent copying GC freed 547964(12MB) AllocSpace objects, 0(0B) LOS objects, 18% free, 25MB/31MB, paused 692us total 484.181ms
I/oject.beautyap: Background concurrent copying GC freed 695852(15MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 32MB/38MB, paused 1.443ms total 1.252s
I/oject.beautyap: NativeAlloc concurrent copying GC freed 685921(14MB) AllocSpace objects, 0(0B) LOS objects, 13% free, 40MB/46MB, paused 720us total 1.206s
I/oject.beautyap: Background concurrent copying GC freed 1128588(24MB) AllocSpace objects, 0(0B) LOS objects, 12% free, 41MB/47MB, paused 799us total 1.067s
I/oject.beautyap: NativeAlloc concurrent copying GC freed 878928(19MB) AllocSpace objects, 0(0B) LOS objects, 9% free, 57MB/63MB, paused 1.591ms total 1.313s
I/oject.beautyap: NativeAlloc concurrent copying GC freed 1436453(31MB) AllocSpace objects, 0(0B) LOS objects, 8% free, 61MB/67MB, paused 695us total 1.480s
I/Choreographer: Skipped 516 frames!  The application may be doing too much work on its main thread.
I/OpenGLRenderer: Davey! duration=8648ms; Flags=0, IntendedVsync=12489910358633, Vsync=12498510358289, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=12498512645661, AnimationStart=12498512736661, PerformTraversalsStart=12498513538661, DrawStart=12498514299661, SyncQueued=12498514614661, SyncStart=12498514757661, IssueDrawCommandsStart=12498514832661, SwapBuffers=12498515692661, FrameCompleted=12498559353661, DequeueBufferDuration=165000, QueueBufferDuration=17743000, 
I/oject.beautyap: NativeAlloc concurrent copying GC freed 1507175(32MB) AllocSpace objects, 0(0B) LOS objects, 12% free, 40MB/46MB, paused 870us total 651.806ms
I/oject.beautyap: Background concurrent copying GC freed 888402(20MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 32MB/38MB, paused 1.804ms total 429.933ms
I/oject.beautyap: Background concurrent copying GC freed 651130(15MB) AllocSpace objects, 0(0B) LOS objects, 17% free, 28MB/34MB, paused 770us total 243.461ms
I/oject.beautyap: Background concurrent copying GC freed 497664(11MB) AllocSpace objects, 0(0B) LOS objects, 16% free, 29MB/35MB, paused 717us total 446.037ms
I/oject.beautyap: Background concurrent copying GC freed 542572(12MB) AllocSpace objects, 0(0B) LOS objects, 17% free, 28MB/34MB, paused 792us total 259.961ms
I/oject.beautyap: Background concurrent copying GC freed 469682(10MB) AllocSpace objects, 0(0B) LOS objects, 16% free, 31MB/37MB, paused 818us total 464.431ms
I/oject.beautyap: Background concurrent copying GC freed 596357(13MB) AllocSpace objects, 0(0B) LOS objects, 17% free, 28MB/34MB, paused 739us total 266.602ms
I/oject.beautyap: Background concurrent copying GC freed 464576(10MB) AllocSpace objects, 0(0B) LOS objects, 16% free, 31MB/37MB, paused 801us total 414.198ms
I/oject.beautyap: Background concurrent copying GC freed 527117(11MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 33MB/39MB, paused 816us total 416.902ms
I/oject.beautyap: Background concurrent copying GC freed 602423(13MB) AllocSpace objects, 0(0B) LOS objects, 14% free, 34MB/40MB, paused 831us total 531.183ms
I/Choreographer: Skipped 530 frames!  The application may be doing too much work on its main thread.
I/OpenGLRenderer: Davey! duration=8864ms; Flags=0, IntendedVsync=12498557124661, Vsync=12507390457641, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=12507402630661, AnimationStart=12507402682661, PerformTraversalsStart=12507402713661, DrawStart=12507403451661, SyncQueued=12507403837661, SyncStart=12507404306661, IssueDrawCommandsStart=12507404467661, SwapBuffers=12507405464661, FrameCompleted=12507422298661, DequeueBufferDuration=579000, QueueBufferDuration=1973000, 
I/oject.beautyap: Background concurrent copying GC freed 624872(13MB) AllocSpace objects, 0(0B) LOS objects, 14% free, 34MB/40MB, paused 1.257ms total 850.488ms
I/oject.beautyap: Background concurrent copying GC freed 642067(14MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 32MB/38MB, paused 850us total 458.440ms
I/oject.beautyap: Background concurrent copying GC freed 533375(11MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 33MB/39MB, paused 760us total 454.370ms
I/oject.beautyap: WaitForGcToComplete blocked NativeAlloc on HeapTrim for 31.920s
I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
I/oject.beautyap: Waiting for a blocking GC ProfileSaver
I/Choreographer: Skipped 75 frames!  The application may be doing too much work on its main thread.
I/OpenGLRenderer: Davey! duration=1272ms; Flags=0, IntendedVsync=12974332351673, Vsync=12975582351623, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=12975590072661, AnimationStart=12975590142661, PerformTraversalsStart=12975591699661, DrawStart=12975598714661, SyncQueued=12975599468661, SyncStart=12975599538661, IssueDrawCommandsStart=12975599641661, SwapBuffers=12975603389661, FrameCompleted=12975605028661, DequeueBufferDuration=91000, QueueBufferDuration=772000, 
I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
I/oject.beautyap: Waiting for a blocking GC Explicit
I/oject.beautyap: NativeAlloc concurrent copying GC freed 3692263(79MB) AllocSpace objects, 0(0B) LOS objects, 2% free, 256MB/262MB, paused 1.233ms total 3.988s
I/oject.beautyap: WaitForGcToComplete blocked ProfileSaver on HeapTrim for 2.935s
I/oject.beautyap: Waiting for a blocking GC Explicit
I/oject.beautyap: WaitForGcToComplete blocked Explicit on ProfileSaver for 992.402ms
I/oject.beautyap: Explicit concurrent copying GC freed 2504022(55MB) AllocSpace objects, 0(0B) LOS objects, 2% free, 258MB/264MB, paused 1.273ms total 4.006s
I/oject.beautyap: WaitForGcToComplete blocked NativeAlloc on ProfileSaver for 21.232s
I/oject.beautyap: NativeAlloc concurrent copying GC freed 2387985(51MB) AllocSpace objects, 0(0B) LOS objects, 2% free, 260MB/266MB, paused 3.574ms total 2.692s
I/oject.beautyap: Waiting for a blocking GC Explicit
I/oject.beautyap: Waiting for a blocking GC Alloc
I/oject.beautyap: NativeAlloc concurrent copying GC freed 2498214(56MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 322MB/328MB, paused 796us total 2.794s
I/oject.beautyap: WaitForGcToComplete blocked Explicit on HeapTrim for 2.226s
I/oject.beautyap: Waiting for a blocking GC Alloc
    
android
firebase
android-edittext
Bradley Juma
Bradley Juma
发布于 2021-03-22
2 个回答
emandt
emandt
发布于 2021-03-26
0 人赞同

似乎sendMessage()是同步的。

你可以到。

final Message message = new Message(String.valueOf(new Random().nextInt(1000)), user, text, new Date());
//v.setEnabled(false);
v.post(new Runnable() {
    @Override
    public void run() {
        sendMessage(message);
        //v.setEnabled(true);

但这样一来,用户可能会很快按下按钮,并排上许多 "sendMessage(...) "请求。为了避免这种情况,你可以在 "v.post() "之前使用 "v.setEnabled(false);",并在 "sendMessage() "之后再次设置为TRUE。

移动到不同的主题。

final Message message = new Message(String.valueOf(new Random().nextInt(1000)), user, text, new Date());
//v.setEnabled(false);
new Thread() {
    @Override
    public void run() {
        sendMessage(message);
        if (v.isAttachedToWindow()) v.post(new Runnable() {
            if (v.isAttachedToWindow()) v.setEnabled(false);
}.start();
    
我不认为sendMessage()是同步的,因为Firebase以异步方式处理所有的请求。我也尝试了你的建议,但问题仍然存在。
如果删除 "sendMessage() "能解决这个问题,那么很有可能是同步的。我认为你必须把 "sendMessage() "移到另一个线程中。我已经编辑了我的答案(这只是一个测试,因为正确的方法是不同的)。
即使把它放在后台线程中,主UI线程仍然冻结。
然后Firebase实例在Main/UiThread中自己/内部运行。如果不知道Firebase的所有方法在内部做什么,就不可能帮助你....。对不起。
你可以使用Android Studio Profiler面板对CPU使用情况进行剖析,以检查谁导致了CPU问题。
Waris Ali Rehmani
Waris Ali Rehmani
发布于 2021-03-26
0 人赞同
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(this, R.layout.activity_layout);
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mDatabaseRef = mFirebaseDatabase.getReference();
send.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String text = chat.getText().toString();
        Message message = new Message(String.valueOf(new Random().nextInt(1000)),new User(),"it will work",new Date());
        sendMessage(message);
        chat.setText("");
private void sendMessage(Message message){