Fix PostedItem loading thread.

Wait loading is finished before destruct.
This commit is contained in:
Phenom 2020-05-10 19:49:07 +02:00
parent cfcd68e661
commit c268f998e6
2 changed files with 63 additions and 32 deletions

View File

@ -36,6 +36,8 @@
#include "ui_PostedItem.h" #include "ui_PostedItem.h"
#include <retroshare/rsposted.h> #include <retroshare/rsposted.h>
#include <chrono>
#include <iostream> #include <iostream>
#define LINK_IMAGE ":/images/thumb-link.png" #define LINK_IMAGE ":/images/thumb-link.png"
@ -46,20 +48,40 @@
// BasePostedItem // // BasePostedItem //
//======================================================================================== //========================================================================================
BasePostedItem::BasePostedItem(FeedHolder *feedHolder, uint32_t feedId, const RsGroupMetaData &group_meta, const RsGxsMessageId& post_id, bool isHome, bool autoUpdate) : BasePostedItem::BasePostedItem( FeedHolder *feedHolder, uint32_t feedId
GxsFeedItem(feedHolder, feedId, group_meta.mGroupId, post_id, isHome, rsPosted, autoUpdate), , const RsGroupMetaData &group_meta, const RsGxsMessageId& post_id
mGroupMeta(group_meta) , bool isHome, bool autoUpdate)
: GxsFeedItem(feedHolder, feedId, group_meta.mGroupId, post_id, isHome, rsPosted, autoUpdate)
, mInFill(false), mGroupMeta(group_meta)
, mLoaded(false), mIsLoadingGroup(false), mIsLoadingMessage(false), mIsLoadingComment(false)
{ {
mPost.mMeta.mMsgId = post_id; mPost.mMeta.mMsgId = post_id;
mPost.mMeta.mGroupId = mGroupMeta.mGroupId; mPost.mMeta.mGroupId = mGroupMeta.mGroupId;
mLoaded = false;
} }
BasePostedItem::BasePostedItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsGroupId &groupId, const RsGxsMessageId& post_id, bool isHome, bool autoUpdate) : BasePostedItem::BasePostedItem( FeedHolder *feedHolder, uint32_t feedId
GxsFeedItem(feedHolder, feedId, groupId, post_id, isHome, rsPosted, autoUpdate) , const RsGxsGroupId &groupId, const RsGxsMessageId& post_id
, bool isHome, bool autoUpdate)
: GxsFeedItem(feedHolder, feedId, groupId, post_id, isHome, rsPosted, autoUpdate)
, mInFill(false)
, mLoaded(false), mIsLoadingGroup(false), mIsLoadingMessage(false), mIsLoadingComment(false)
{ {
mPost.mMeta.mMsgId = post_id; mPost.mMeta.mMsgId = post_id;
mLoaded = false; }
BasePostedItem::~BasePostedItem()
{
auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(200);
while( (mIsLoadingGroup || mIsLoadingMessage || mIsLoadingComment)
&& std::chrono::steady_clock::now() < timeout)
{
RsDbg() << __PRETTY_FUNCTION__ << " is Waiting "
<< (mIsLoadingGroup ? "Group " : "")
<< (mIsLoadingMessage ? "Message " : "")
<< (mIsLoadingComment ? "Comment " : "")
<< "loading finished." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
} }
void BasePostedItem::paintEvent(QPaintEvent *e) void BasePostedItem::paintEvent(QPaintEvent *e)
@ -96,6 +118,7 @@ bool BasePostedItem::setPost(const RsPostedPost &post, bool doFill)
void BasePostedItem::loadGroup() void BasePostedItem::loadGroup()
{ {
mIsLoadingGroup = true;
RsThread::async([this]() RsThread::async([this]()
{ {
// 1 - get group data // 1 - get group data
@ -110,13 +133,14 @@ void BasePostedItem::loadGroup()
if(!rsPosted->getBoardsInfo(groupIds,groups)) if(!rsPosted->getBoardsInfo(groupIds,groups))
{ {
RsErr() << "GxsPostedGroupItem::loadGroup() ERROR getting data" << std::endl; RsErr() << "GxsPostedGroupItem::loadGroup() ERROR getting data" << std::endl;
mIsLoadingGroup = false;
return; return;
} }
if (groups.size() != 1) if (groups.size() != 1)
{ {
std::cerr << "GxsPostedGroupItem::loadGroup() Wrong number of Items"; std::cerr << "GxsPostedGroupItem::loadGroup() Wrong number of Items" << std::endl;
std::cerr << std::endl; mIsLoadingGroup = false;
return; return;
} }
RsPostedGroup group(groups[0]); RsPostedGroup group(groups[0]);
@ -127,7 +151,8 @@ void BasePostedItem::loadGroup()
* thread, for example to update the data model with new information * thread, for example to update the data model with new information
* after a blocking call to RetroShare API complete */ * after a blocking call to RetroShare API complete */
mGroupMeta = group.mMeta; mGroupMeta = group.mMeta;
mIsLoadingGroup = false;
}, this ); }, this );
}); });
@ -135,6 +160,7 @@ void BasePostedItem::loadGroup()
void BasePostedItem::loadMessage() void BasePostedItem::loadMessage()
{ {
mIsLoadingMessage = true;
RsThread::async([this]() RsThread::async([this]()
{ {
// 1 - get group data // 1 - get group data
@ -145,15 +171,16 @@ void BasePostedItem::loadMessage()
if(! rsPosted->getBoardContent( groupId(), std::set<RsGxsMessageId>( { messageId() } ),posts,comments)) if(! rsPosted->getBoardContent( groupId(), std::set<RsGxsMessageId>( { messageId() } ),posts,comments))
{ {
RsErr() << "BasePostedItem::loadMessage() ERROR getting data" << std::endl; RsErr() << "BasePostedItem::loadMessage() ERROR getting data" << std::endl;
mIsLoadingMessage = false;
return; return;
} }
if (posts.size() == 1) if (posts.size() == 1)
{ {
std::cerr << (void*)this << ": Obtained post, with msgId = " << posts[0].mMeta.mMsgId << std::endl; std::cerr << (void*)this << ": Obtained post, with msgId = " << posts[0].mMeta.mMsgId << std::endl;
const RsPostedPost& post(posts[0]); const RsPostedPost& post(posts[0]);
RsQThreadUtils::postToObject( [post,this]() { setPost(post,true); }, this ); RsQThreadUtils::postToObject( [post,this]() { setPost(post,true); mIsLoadingMessage = false;}, this );
} }
else if(comments.size() == 1) else if(comments.size() == 1)
{ {
@ -162,21 +189,21 @@ void BasePostedItem::loadMessage()
RsQThreadUtils::postToObject( [cmt,this]() RsQThreadUtils::postToObject( [cmt,this]()
{ {
setComment(cmt); setComment(cmt);
//Change this item to be uploaded with thread element. //Change this item to be uploaded with thread element.
setMessageId(cmt.mMeta.mThreadId); setMessageId(cmt.mMeta.mThreadId);
requestMessage(); requestMessage();
mIsLoadingMessage = false;
}, this ); }, this );
} }
else else
{ {
std::cerr << "GxsChannelPostItem::loadMessage() Wrong number of Items. Remove It."; std::cerr << "GxsChannelPostItem::loadMessage() Wrong number of Items. Remove It." << std::endl;
std::cerr << std::endl;
RsQThreadUtils::postToObject( [this]() { removeItem(); }, this ); RsQThreadUtils::postToObject( [this]() { removeItem(); mIsLoadingMessage = false;}, this );
} }
}); });
} }
@ -188,7 +215,7 @@ void BasePostedItem::loadComment()
std::cerr << "GxsChannelPostItem::loadComment()"; std::cerr << "GxsChannelPostItem::loadComment()";
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
mIsLoadingComment = true;
RsThread::async([this]() RsThread::async([this]()
{ {
// 1 - get group data // 1 - get group data
@ -204,15 +231,16 @@ void BasePostedItem::loadComment()
if(! rsPosted->getBoardContent( groupId(),msgIds,posts,comments)) if(! rsPosted->getBoardContent( groupId(),msgIds,posts,comments))
{ {
RsErr() << "BasePostedItem::loadGroup() ERROR getting data" << std::endl; RsErr() << "BasePostedItem::loadGroup() ERROR getting data" << std::endl;
mIsLoadingComment = false;
return; return;
} }
int comNb = comments.size(); int comNb = comments.size();
RsQThreadUtils::postToObject( [comNb,this]() RsQThreadUtils::postToObject( [comNb,this]()
{ {
setCommentsSize(comNb); setCommentsSize(comNb);
mIsLoadingComment = false;
}, this ); }, this );
}); });
} }

View File

@ -31,7 +31,7 @@ class PostedItem;
} }
class FeedHolder; class FeedHolder;
class RsPostedPost; struct RsPostedPost;
class BasePostedItem : public GxsFeedItem class BasePostedItem : public GxsFeedItem
{ {
@ -40,7 +40,7 @@ class BasePostedItem : public GxsFeedItem
public: public:
BasePostedItem(FeedHolder *parent, uint32_t feedId, const RsGxsGroupId& groupId, const RsGxsMessageId& messageId, bool isHome, bool autoUpdate); BasePostedItem(FeedHolder *parent, uint32_t feedId, const RsGxsGroupId& groupId, const RsGxsMessageId& messageId, bool isHome, bool autoUpdate);
BasePostedItem(FeedHolder *parent, uint32_t feedId, const RsGroupMetaData& group_meta, const RsGxsMessageId& post_id, bool isHome, bool autoUpdate); BasePostedItem(FeedHolder *parent, uint32_t feedId, const RsGroupMetaData& group_meta, const RsGxsMessageId& post_id, bool isHome, bool autoUpdate);
virtual ~BasePostedItem()=default; virtual ~BasePostedItem();
bool setPost(const RsPostedPost& post, bool doFill = true); bool setPost(const RsPostedPost& post, bool doFill = true);
@ -64,15 +64,15 @@ protected:
virtual void paintEvent(QPaintEvent *) override; virtual void paintEvent(QPaintEvent *) override;
/* GxsGroupFeedItem */ /* GxsGroupFeedItem */
virtual QString groupName(); virtual QString groupName() override;
virtual void loadGroup() override; virtual void loadGroup() override;
virtual RetroShareLink::enumType getLinkType() { return RetroShareLink::TYPE_UNKNOWN; } virtual RetroShareLink::enumType getLinkType() override { return RetroShareLink::TYPE_UNKNOWN; }
/* GxsFeedItem */ /* GxsFeedItem */
virtual QString messageName(); virtual QString messageName() override;
virtual void loadMessage(); virtual void loadMessage() override;
virtual void loadComment(); virtual void loadComment() override;
bool mInFill; bool mInFill;
RsGroupMetaData mGroupMeta; RsGroupMetaData mGroupMeta;
@ -80,7 +80,7 @@ protected:
virtual void setup()=0; virtual void setup()=0;
virtual void fill()=0; virtual void fill()=0;
virtual void doExpand(bool open)=0; virtual void doExpand(bool open) override =0;
virtual void setComment(const RsGxsComment&)=0; virtual void setComment(const RsGxsComment&)=0;
virtual void setReadStatus(bool isNew, bool isUnread)=0; virtual void setReadStatus(bool isNew, bool isUnread)=0;
virtual void setCommentsSize(int comNb)=0; virtual void setCommentsSize(int comNb)=0;
@ -89,7 +89,10 @@ protected:
virtual void toggleNotes()=0; virtual void toggleNotes()=0;
private: private:
bool mLoaded; bool mLoaded;
bool mIsLoadingGroup;
bool mIsLoadingMessage;
bool mIsLoadingComment;
}; };
class PostedItem: public BasePostedItem class PostedItem: public BasePostedItem