Duplicate threads showing on the Conversation fragment
Hello again!
As the title suggests, the threads list keeps duplicating the conversation but the data is still the same so it shouldn't be to do with it reading another list or anything and it should also have nothing to do with the full thread delete code either as that still removes the thread from both the Firebase database and local! So i don't know what's going on, i have a feeling i have changed a line of code and that's why but i compared what i have now to the stock code and there's no changes to the threads list! Or at least from what i could see.
So as always any help would be appreciated
Thanks Ryan
Quick update... The conversations only seem to be duplicated on the user that created the thread
Okay now for some reason the conversation is duplicated on the received user but only when a conversation has been deleted then another created straight after
To me it sounds like you've made a change to the code which has caused this bug. The app will download the threads from Firebase and then add them to the internal database. So even if you haven't modified the threads view, it could be that you changed some other which is causing threads to be stored twice. Without looking at your project, it would be hard to propose a fix. I'd recommend looking at earlier commits to see where you introduced this problem and then looking at the Git diff to see which code has been changed.
Try to this.
BChatcatNetworkAdapter
@Override
public Promise<BThread, BError, Void> createThreadWithUsers(final String name, final List<BUser> users) {
final Deferred<BThread, BError, Void> deferred = new DeferredObject<>();
ThreadRecovery.checkForAndRecoverThreadWithUsers(users)
.done(new DoneCallback<BThread>() {
@Override
public void onDone(final BThread thread) {
+ if (users.size() <= 2 || StringUtils.equalsIgnoreCase(thread.getName(), name)) {
deferred.resolve(thread);
} else {
createThreadWithUsers(deferred, name, users);
}
}
})
.fail(new FailCallback<BError>() {
@Override
public void onFail(BError error) {
createThreadWithUsers(deferred, name, users);
}
});
return deferred.promise();
}
@Override
public Promise<Void, BError, Void> deleteThreadWithEntityID(final String entityID) {
final BThread thread = DaoCore.fetchEntityWithEntityID(BThread.class, entityID);
+ if (thread == null) {
+ final Deferred<Void, BError, Void> deferred = new DeferredObject<>();
+ deferred.resolve(null);
+ return deferred.promise();
+ }
updateLastOnline();
return new BThreadWrapper(thread).deleteThread();
}
BThreadWrapper
public class BThreadWrapper extends EntityWrapper<BThread> {
+ protected BThread model;
+ private String entityId;
public static final boolean DEBUG = Debug.BThread;
public BThreadWrapper(BThread thread){
this.model = thread;
this.entityId = thread.getEntityID();
}
//Note - Maybe should treat group thread and one on one thread the same
/**
* Deleting a thread, Thread isn't always actually deleted from the db.
* We mark the thread as deleted and mark the user in the thread users ref as deleted.
**/
public Promise<Void, BError, Void> deleteThread(){
if (DEBUG) Timber.v("deleteThread");
final Deferred<Void, BError, Void> deferred = new DeferredObject<>();
BUser user = getNetworkAdapter().currentUserModel();
if (model.getTypeSafely() != BThreadEntity.Type.Private) return deferred.promise();
List<BMessage> messages = DaoCore.fetchEntitiesWithProperty(BMessage.class, BMessageDao.Properties.ThreadDaoId, model.getId());
for (BMessage m : messages) {
DaoCore.deleteEntity(m);
}
DaoCore.updateEntity(model);
if (model.getUsers().size() > 2)
{
// Removing the thread from the current user thread ref.
DatabaseReference userThreadRef = FirebasePaths.firebaseRef()
.child(user.getBPath().getPath())
.child(model.getBPath().getPath());
// Stop listening to thread events, Details change, User added and incoming messages.
off();
messagesOff();
usersOff();
// Removing the thread from the user threads list.
userThreadRef.removeValue(new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference firebase) {
// Delete the thread if no error occurred when deleting from firebase.
if (error == null)
{
// Adding a leave value to the user on the thread path so other users will know this user has left.
DatabaseReference threadUserRef = FirebasePaths.threadRef(entityId)
.child(BFirebaseDefines.Path.BUsersPath)
.child(getNetworkAdapter().currentUserModel().getEntityID())
.child(BDefines.Keys.BLeaved);
threadUserRef.setValue(true);
List<UserThreadLink> list = DaoCore.fetchEntitiesWithProperty(UserThreadLink.class, UserThreadLinkDao.Properties.BThreadDaoId, model.getId());
DaoCore.deleteEntity(model);
// Deleting all data relevant to the thread from the db.
for (UserThreadLink d : list)
DaoCore.deleteEntity(d);
if (DEBUG)
{
BThread deletedThread = DaoCore.fetchEntityWithEntityID(BThread.class, entityId);
if (deletedThread == null)
Timber.d("Thread deleted successfully.");
else Timber.d("Thread was not deleted.");
}
deferred.resolve(null);
} else
{
deferred.reject(getFirebaseError(error));
}
}
});
}
else
{
DatabaseReference threadUserRef = FirebasePaths.threadRef(entityId)
.child(BFirebaseDefines.Path.BUsersPath)
.child(getNetworkAdapter().currentUserModel().getEntityID())
.child(BDefines.Keys.BDeleted);
// Set the deleted value in firebase
threadUserRef.setValue(ServerValue.TIMESTAMP);
model.setDeleted(true);
DaoCore.updateEntity(model);
// DELETE THIS CODE OR COMMENT
// DaoCore.deleteEntity(model);
deferred.resolve(null);
}
return deferred.promise();
Add a comment to this line
}
/**
* Add the thread from the given user threads ref.
**/
public Promise<BThread, BError, Void> addUserWithEntityID(String entityId){
final Deferred<BThread, BError, Void> deferred = new DeferredObject<>();
DatabaseReference ref = FirebasePaths.threadRef(this.entityId)
.child(BFirebaseDefines.Path.BUsersPath)
+ .child(entityId)
+ .child(BDefines.Keys.BName);
BUser user = DaoCore.fetchOrCreateEntityWithEntityID(BUser.class, entityId);
// If metaname is null the data wont be saved so we have to do so.
+ String name = user.getMetaName() == null ? "no_name" : user.getMetaName();
+ ref.setValue((name), new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError firebaseError, DatabaseReference firebase) {
if (firebaseError == null)
deferred.resolve(BThreadWrapper.this.model);
else
deferred.reject(getFirebaseError(firebaseError));
}
});
return deferred.promise();
}
ChatSDKPickFriendsActivity
@Override
protected void onResume() {
super.onResume();
initList();
listContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showProgDialog(getString(R.string.open_chat));
listContacts.setOnItemClickListener(null);
final AdapterView.OnItemClickListener onItemClickListener = this;
createAndOpenThreadWithUsers(listAdapter.getItem(position).asBUser().getMetaName(), listAdapter.getItem(position).asBUser(), getNetworkAdapter().currentUserModel())
.done(new DoneCallback<BThread>() {
@Override
public void onDone(BThread result) {
finish();
}
})
.fail(new FailCallback<BError>() {
@Override
public void onFail(BError result) {
dismissProgDialog();
listContacts.setOnItemClickListener(onItemClickListener);
}
Add a comment to this line
});
AbstractNetworkAdapter
public <E extends ChatSDKAbstractThreadsListAdapter.ThreadListItem> List<E> threadItemsWithType(int threadType, ChatSDKAbstractThreadsListAdapter.ThreadListItemMaker<E> itemMaker) {
if (DEBUG) Timber.v("threadItemsWithType, Type: %s", threadType);
if (currentUserModel() == null) {
if (DEBUG) Timber.e("threadItemsWithType, Current user is null");
return null;
}
BUser currentUser = currentUserModel(), threadCreator;
// Get the thread list ordered desc by the last message added date.
List<BThread> threadsFromDB;
if (threadType == BThread.Type.Private)
{
if (DEBUG) Timber.v("threadItemsWithType, loading private.");
threadsFromDB = currentUserModel().getThreads(BThread.Type.Private);
}
else threadsFromDB = DaoCore.fetchEntitiesWithProperty(BThread.class, BThreadDao.Properties.Type, threadType);
fixCopyThreads(threadsFromDB);
List<E> threads = new ArrayList<E>();
if (DEBUG) Timber.v("threadItemsWithType, size: " + threadsFromDB.size());
if (threadType == BThread.Type.Public)
{
for (BThread thread : threadsFromDB)
if (thread.getTypeSafely() == BThread.Type.Public)
threads.add(itemMaker.fromBThread(thread));
}
else {
for (BThread thread : threadsFromDB) {
if (DEBUG) Timber.i("threadItemsWithType, ThreadID: %s", thread.getId());
if (thread.isDeleted())
continue;
if (thread.getMessagesWithOrder(DaoCore.ORDER_DESC).size() > 0)
{
threads.add(itemMaker.fromBThread(thread));
continue;
}
else if (DEBUG) Timber.e("threadItemsWithType, Thread has no messages.");
+ if (StringUtils.isNotBlank(thread.getCreatorEntityId()) && thread.getCreatorEntityId().equals(currentUser.getEntityID()))
{
threads.add(itemMaker.fromBThread(thread));
}
else
{
threadCreator = thread.getCreator();
if (threadCreator != null )
{
if (DEBUG) Timber.d("thread has creator. Entity ID: %s", thread.getEntityID());
if (threadCreator.equals(currentUser) && thread.hasUser(currentUser))
{
if (DEBUG) Timber.d("Current user is the creator.");
threads.add(itemMaker.fromBThread(thread));
}
}
}
}
}
if (DEBUG) Timber.v("threadItemsWithType, Type: %s, Found on db: &s, Threads List Size: %s",
threadType, threadsFromDB.size(), threads.size());
Collections.sort(threads, new ThreadsItemSorter());
return threads;
}
private void fixCopyThreads(List<BThread> threadsFromDB) {
List<BThread> toRemove = new ArrayList<>();
for (int i = 0; i < threadsFromDB.size() - 1; i++) {
BThread thread1 = threadsFromDB.get(i);
for (int j = i + 1; j < threadsFromDB.size(); j++) {
BThread thread2 = threadsFromDB.get(j);
+ if (thread1.getEntityID().equals(thread2.getEntityID()) ) {
toRemove.add(thread2);
}
}
I'm going to be looking into the problem when i get started on the update after I've released the iOS version.
It's likely to do with my own code, I'm probably showing the threads from the local database plus Firebase because i changed the way contacts are read and i think i may have done the same to the threads too.
Thank you @R12rus I'll keep the code in mind just in case i need it! and @bensmiley If it doesn't end up being my code then could you email me when or if it's fixed.
Thanks Ryan
Any news about that issue? I personally didn't changed the sample code but I have that issue too.
Try using my code above .
I know why it wasn't working for me. I had messed about with it at some point.
If i remember correctly, i had changed the thread type to public rather than private and it started to cause issues, so look for any of these adapter.setThreadItems(BNetworkManager.sharedManager().getNetworkAdapter().threadItemsWithType(BThread.Type.Private, adapter.getItemMaker()));
on the ChatSDKAbstractConversationsFragment and make sure BThread.Type is in fact private!
I have the same problem. I put adapter.setThreadItems(BNetworkManager.sharedManager().getNetworkAdapter().threadItemsWithType(BThread.Type.Private, adapter.getItemMaker())); but nothing change. Any idea?
Fixed in v4.0.