Sunday, 1 September 2019

Thread conflict Observer pattern for notification management

I'm developing an android application that have to receive some notification. I receive this notification between firebase and I had create a dispatcher object for update some information in some Fragment or Activity.

I'm trying to use Observer pattern for this pourpose.

I had create an interface called Update.

public interface Updateable {

    void update();

    void update(int caller);

    void update(int caller, Object obj);
}

The Fragment or Activity are the Observer, and implement Update.

The notification dispatcher is the Subject, that store Updateable Object and notify that when the FirebaseMessagingService pass to it a notification.

My problem is, when I visualize an Updateable Activity Chat and I receive a notification that have to update Chat I have a thread conflicts and the crash.

This is the log about Chat Activity.

 2019-08-26 17:38:41.668 1193-1440/? W/InputDispatcher: channel 'd733762 it.myapp/it.myapp.Chat (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
 2019-08-26 17:38:41.668 1193-1440/? E/InputDispatcher: channel 'd733762 it.myapp/it.myapp.Chat (server)' ~ Channel is unrecoverably broken and will be disposed!
 2019-08-26 17:38:41.750 1193-2760/? I/WindowManager: WIN DEATH: Window{d733762 u0 it.myapp/it.myapp.Chat}
 2019-08-26 17:38:41.750 1193-2760/? V/WindowManager: removeIfPossible: Window{d733762 u0 it.myapp/it.myapp.Chat}
 2019-08-26 17:38:41.750 1193-2760/? W/InputDispatcher: Attempted to unregister already unregistered input channel 'd733762 it.myapp/it.myapp.Chat (server)'
 2019-08-26 17:38:41.750 1193-2760/? V/WindowManager: Not removing Window{d733762 u0 it.myapp/it.myapp.Chat EXITING} due to exit animation 
 2019-08-26 17:38:41.772 1193-11657/? V/WindowManager: Exit animation finished in Window{d733762 u0 it.myapp/it.myapp.Chat EXITING}: remove=true
 2019-08-26 17:38:41.773 1193-11657/? E/WindowManager: win=Window{d733762 u0 it.myapp/it.myapp.Chat EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true
 2019-08-26 17:38:41.773 1193-11657/? W/WindowManager: Exception thrown when updateSurfaceStatusSurface(name=it.myapp/it.myapp.Chat)/@0xafbd060: android.os.DeadObjectException
 2019-08-26 17:38:41.774 1193-11657/? V/WindowManager: postWindowRemoveCleanupLocked: Window{d733762 u0 it.myapp/it.myapp.Chat}
 2019-08-26 17:38:41.774 1193-11657/? V/WindowManager: Removing Window{d733762 u0 it.myapp/it.myapp.Chat} from AppWindowToken{74a1a5f token=Token{39042fe ActivityRecord{f6f3eb9 u0 it.myapp/.Chat t1826}}}
 2019-08-26 17:38:41.782 1193-11657/? V/WindowManager: removeAppToken: AppWindowToken{74a1a5f token=Token{39042fe ActivityRecord{f6f3eb9 u0 it.myapp/.Chat t1826}}} delayed=true Callers=com.android.server.wm.DisplayContent.removeAppToken:1086 com.android.server.wm.AppWindowContainerController.removeContainer:315 com.android.server.am.ActivityRecord.removeWindowContainer:1201 com.android.server.am.ActivityStack.removeActivityFromHistoryLocked:4568 
 2019-08-26 17:38:41.817 2085-4155/? E/DollieAdapterService: notifyActivityState pkg:it.myapp/it.myapp.Chat state:18 fg:false mUid:10162
 2019-08-26 17:38:41.817 1193-1299/? I/StatusBarDisable: setFlags what=0 which=1 pkg=Window{d733762 u0 it.myapp/it.myapp.Chat}
 2019-08-26 17:38:41.817 1193-1299/? I/StatusBarDisable: setFlags what=0 which=1 pkg=Window{d733762 u0 it.myapp/it.myapp.Chat}
 2019-08-26 17:38:41.818 1193-1299/? I/StatusBarDisable: setFlags what=0 which=1 pkg=Window{d733762 u0 it.myapp/it.myapp.Chat}

this is the Chat Activity

public class Chat extends AppCompatActivity implements Loader, Updateable  {

    private ArrayList<Message> mess;

    private RecyclerView mex_list;
    private RecyclerView.Adapter mAdapter;
    private final LinearLayoutManager llm = new LinearLayoutManager(this);;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat);
    initGui();
    NotificationEventDispatcher.registry(this, NotificationEventDispatcher.CH_4_0);

    mAdapter = new ChatAdapter(this,mess);
    llm.setStackFromEnd(true);
    mAdapter.setHasStableIds(false);
    mex_list.setLayoutManager(llm);
    mex_list.setAdapter(mAdapter);
    /*
    *
    */

}


/*
*
*/


@Override
public void update() {

}

@Override
public void update(int caller) {

}

@Override
public void update(final int caller, final Object obj) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            RemoteMessage rm = (RemoteMessage) obj;
            Message m = createMessageWithRemoteMessageInfo(rm):

            mess.add(m);

            mAdapter.notifyItemInserted(mess.size() - 1);
            if (llm.findLastCompletelyVisibleItemPosition() == mess.size() - 2)
                   llm.scrollToPosition(mess.size() - 1);

            }
    });

}
}

It's really unusual problem, I really don't know how to fix without destroy the Observer architecture.

Every advice is valued. Thanks.



from Thread conflict Observer pattern for notification management

No comments:

Post a Comment