Wordpress-ios: Comments and comment count are not updated when leaving comments via Reader view

Created on 22 Jan 2020  ·  2Comments  ·  Source: wordpress-mobile/WordPress-iOS

Steps to reproduce:

  1. Open any post in the Reader and leave a comment.
  2. Check to see if the comment shows up.
  3. Go back one screen and look at the comment count.
  4. Check to see if the comment count matches the actual number of comments.
  5. Go back another screen.
  6. Re-open the view with the comment count and then the comments list and find that both are now updated.

Result: changes to comments don't show up in the Reader unless you leave and go back. (1m33s)

wrong-comment-count-wpios-14 0 0 0
Tested with WPiOS 14.0.0.0 TestFlight beta on iPhone 6S iOS 13.3.

Beta Request Comments Reader [Type] Enhancement

Most helpful comment

After some extensive research into this issue I've found the following.

Also tagging @aerych to help verify my findings.

tl;dr

There is an issue in the app where Reader Posts and Comments are duplicated in the database.

Cause of the issue

The root cause of this issue is due to the way the reader creates/updates posts based on a topic. Due to this the reader will create a new post for each different topic which looks something like this:

  1. User launches the app and opens the reader on the Followed Sites view
  2. The app pulls down all the new posts and does a check to see if it exists already for the topic:

Find the posts where the global ID is equal to '1' and the topic is equal to 'default'

NSString *globalID = remotePost.globalID;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"ReaderPost"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"globalID = %@ AND (topic = %@ OR topic = NULL)", globalID, topic];
  1. If no results are returned a new post object is inserted into CoreData

If the user then taps on the site name to open the site detail view the following happens except for a different topic such as:

Find the posts where the global ID is equal to '1' and the topic is equal to 'site-id'

This will return 0 results since the previous post was added for a different topic (followed sites), so the app will then insert a post object for that specific topic.

The same applies for posts loaded via Search, Tags, Discovery, etc because they are all different topics.

Looking at the database itself you can see this duplication of posts (I'm using emojies as the post title for visual reference):

Screen Shot 2020-02-05 at 11 34 07 AM

How does this relate to the comments?

Since we now know that each area a post can appear there is a different Post object, this means that when a comment is created it will attempt to be created for that Post object.

I say attempt because, when a user posts a comment or reply the following method is called:

- (Comment *)createHierarchicalCommentWithContent:(NSString *)content
                                       withParent:(NSNumber *)parentID
                                           postID:(NSNumber *)postID
                                           siteID:(NSNumber *)siteID

Since this method does not take a Post object it instead attempts to find the post that the comment should be attached to using the postID and siteID and grabs the first result returned.

// Fetch the relevant ReaderPost
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([ReaderPost class])];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"postID = %@ AND siteID = %@", postID, siteID];
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error) {
    DDLogError(@"Error fetching post with id %@ and site %@. %@", postID, siteID, error);
    return nil;
}

ReaderPost *post = [results firstObject];
if (!post) {
    return nil;
}

This means the Comment could not be attached to the right post which means it won't appear until a network fetch is done and a new Comment object is created for the right Post object.

The broader issue

Diving a bit deeper into this I can see a few issues:

  • Post objects are duplicated, for larger posts this can cause an issue with the amount of data we're storing.
  • Comments are duplicated, for longer comment threads this can be an issue with data storage.
  • You can save the same post multiple times

src="https://user-images.githubusercontent.com/793774/73878096-ca5c2580-480e-11ea-953a-79fd9e043a7b.png">

All 2 comments

After some extensive research into this issue I've found the following.

Also tagging @aerych to help verify my findings.

tl;dr

There is an issue in the app where Reader Posts and Comments are duplicated in the database.

Cause of the issue

The root cause of this issue is due to the way the reader creates/updates posts based on a topic. Due to this the reader will create a new post for each different topic which looks something like this:

  1. User launches the app and opens the reader on the Followed Sites view
  2. The app pulls down all the new posts and does a check to see if it exists already for the topic:

Find the posts where the global ID is equal to '1' and the topic is equal to 'default'

NSString *globalID = remotePost.globalID;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"ReaderPost"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"globalID = %@ AND (topic = %@ OR topic = NULL)", globalID, topic];
  1. If no results are returned a new post object is inserted into CoreData

If the user then taps on the site name to open the site detail view the following happens except for a different topic such as:

Find the posts where the global ID is equal to '1' and the topic is equal to 'site-id'

This will return 0 results since the previous post was added for a different topic (followed sites), so the app will then insert a post object for that specific topic.

The same applies for posts loaded via Search, Tags, Discovery, etc because they are all different topics.

Looking at the database itself you can see this duplication of posts (I'm using emojies as the post title for visual reference):

Screen Shot 2020-02-05 at 11 34 07 AM

How does this relate to the comments?

Since we now know that each area a post can appear there is a different Post object, this means that when a comment is created it will attempt to be created for that Post object.

I say attempt because, when a user posts a comment or reply the following method is called:

- (Comment *)createHierarchicalCommentWithContent:(NSString *)content
                                       withParent:(NSNumber *)parentID
                                           postID:(NSNumber *)postID
                                           siteID:(NSNumber *)siteID

Since this method does not take a Post object it instead attempts to find the post that the comment should be attached to using the postID and siteID and grabs the first result returned.

// Fetch the relevant ReaderPost
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([ReaderPost class])];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"postID = %@ AND siteID = %@", postID, siteID];
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error) {
    DDLogError(@"Error fetching post with id %@ and site %@. %@", postID, siteID, error);
    return nil;
}

ReaderPost *post = [results firstObject];
if (!post) {
    return nil;
}

This means the Comment could not be attached to the right post which means it won't appear until a network fetch is done and a new Comment object is created for the right Post object.

The broader issue

Diving a bit deeper into this I can see a few issues:

  • Post objects are duplicated, for larger posts this can cause an issue with the amount of data we're storing.
  • Comments are duplicated, for longer comment threads this can be an issue with data storage.
  • You can save the same post multiple times

src="https://user-images.githubusercontent.com/793774/73878096-ca5c2580-480e-11ea-953a-79fd9e043a7b.png">

Sound spot on. :) Each topic gets its own set of posts and this leads allows for the scenario of one post having a different like status or comment count compared the same post in a different topic. (But corrected as soon as the topic (or post) is synced again.
Its been a known issue that comes up infrequently (maybe a handful of comments in something like four years?) and so something we haven't prioritized to patch.

Was this page helpful?
0 / 5 - 0 ratings