Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions WordPress/Classes/Services/ReaderPostService.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ extern NSString * const ReaderPostServiceToggleSiteFollowingState;

@interface ReaderPostService : LocalCoreDataService

@property (copy, nonatomic) NSString *nextPageHandle;
@property (nonatomic) NSInteger pageNumber;

/**
Fetches the posts for the specified topic

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import Foundation

extension ReaderPostService {
func fetchPostsV2(for topic: ReaderTagTopic, isFirstPage: Bool = true, success: @escaping (Int, Bool) -> Void, failure: @escaping (Error?) -> Void) {
class ReaderPostStreamService {

private let coreDataStack: CoreDataStack

private var nextPageHandle: String?
private var pageNumber: Int = 0

init(coreDataStack: CoreDataStack) {
self.coreDataStack = coreDataStack
}

func fetchPosts(for topic: ReaderTagTopic, isFirstPage: Bool = true, success: @escaping (Int, Bool) -> Void, failure: @escaping (Error?) -> Void) {
if isFirstPage {
nextPageHandle = nil
}

let remoteService = ReaderPostServiceRemote.withDefaultApi()
remoteService.fetchPosts(for: [topic.slug], page: nextPageHandle, success: { posts, pageHandle in
self.managedObjectContext.perform {

if self.managedObjectContext.parent == ContextManager.shared.mainContext {
// Its possible the ReaderAbstractTopic was deleted the parent main context.
// If so, and we merge and save, it will cause a crash.
// Reset the context so it will be current with its parent context.
self.managedObjectContext.reset()
}

guard let readerTopic = try? self.managedObjectContext.existingObject(with: topic.objectID) as? ReaderAbstractTopic else {
self.coreDataStack.performAndSave { context in
guard let readerTopic = try? context.existingObject(with: topic.objectID) as? ReaderAbstractTopic else {
// if there was an error or the topic was deleted just bail.
success(0, false)
return
Expand All @@ -27,44 +29,41 @@ extension ReaderPostService {

if isFirstPage {
self.pageNumber = 1
self.removePosts(forTopic: readerTopic)
self.removePosts(forTopic: readerTopic, in: context)
} else {
self.pageNumber += 1
}

posts.enumerated().forEach { index, remotePost in
let post = ReaderPost.createOrReplace(fromRemotePost: remotePost, for: readerTopic, context: self.managedObjectContext)
let post = ReaderPost.createOrReplace(fromRemotePost: remotePost, for: readerTopic, context: context)
// To keep the API order
post?.sortRank = NSNumber(value: Date().timeIntervalSinceReferenceDate - Double(((self.pageNumber * Constants.paginationMultiplier) + index)))
}

// Clean up
self.deletePostsInExcessOfMaxAllowed(for: readerTopic)
self.deletePostsFromBlockedSites()

ContextManager.shared.save(self.managedObjectContext) {
let hasMore = pageHandle != nil
success(posts.count, hasMore)
}

let serivce = ReaderPostService(managedObjectContext: context)
serivce.deletePostsInExcessOfMaxAllowed(for: readerTopic)
serivce.deletePostsFromBlockedSites()
Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed that ReaderPostService will be retained during this operation? Do we not need to keep a reference to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By keeping a reference, do you mean saving it a stored property? If that's the case, I don't think we need to do that. With changes in this PR, ReaderPostSerivce is now stateless, like almost all other "service" types. So, it can be used in a create-and-discard way, like almost all other "service" types.

} completion: {
let hasMore = pageHandle != nil
success(posts.count, hasMore)
}

}, failure: { error in
failure(error)
})
}

private func removePosts(forTopic topic: ReaderAbstractTopic) {
private func removePosts(forTopic topic: ReaderAbstractTopic, in context: NSManagedObjectContext) {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: ReaderPost.classNameWithoutNamespaces())
fetchRequest.predicate = NSPredicate(format: "topic == %@", argumentArray: [topic])
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "sortRank", ascending: false)]
fetchRequest.returnsObjectsAsFaults = false

do {
let results = try managedObjectContext.fetch(fetchRequest)
let results = try context.fetch(fetchRequest)
for object in results {
guard let objectData = object as? NSManagedObject else { continue }
managedObjectContext.delete(objectData)
context.delete(objectData)

// Checar se ta em uso ou foi salvo
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import Combine
private var noResultsStatusViewController = NoResultsViewController.controller()
private var noFollowedSitesViewController: NoResultsViewController?

private lazy var readerPostStreamService = ReaderPostStreamService(coreDataStack: coreDataStack)

var resultsStatusView: NoResultsViewController {
get {
guard let noFollowedSitesVC = noFollowedSitesViewController else {
Expand Down Expand Up @@ -1048,12 +1050,13 @@ import Combine
return
}

let service = ReaderPostService(managedObjectContext: context)
if ReaderHelpers.isTopicSearchTopic(topic) {
let service = ReaderPostService(managedObjectContext: context)
service.fetchPosts(for: topic, atOffset: 0, deletingEarlier: false, success: success, failure: failure)
} else if let topic = topic as? ReaderTagTopic {
service.fetchPostsV2(for: topic, success: success, failure: failure)
self.readerPostStreamService.fetchPosts(for: topic, success: success, failure: failure)
} else {
let service = ReaderPostService(managedObjectContext: context)
service.fetchPosts(for: topic, earlierThan: Date(), success: success, failure: failure)
}
}
Expand Down Expand Up @@ -1168,13 +1171,14 @@ import Combine
return
}

let service = ReaderPostService(managedObjectContext: context)
if ReaderHelpers.isTopicSearchTopic(topic) {
let service = ReaderPostService(managedObjectContext: context)
let offset = UInt(self.content.contentCount)
service.fetchPosts(for: topic, atOffset: UInt(offset), deletingEarlier: false, success: success, failure: failure)
} else if let topic = topic as? ReaderTagTopic {
service.fetchPostsV2(for: topic, isFirstPage: false, success: success, failure: failure)
self.readerPostStreamService.fetchPosts(for: topic, isFirstPage: false, success: success, failure: failure)
} else {
let service = ReaderPostService(managedObjectContext: context)
let earlierThan = sortDate
service.fetchPosts(for: topic, earlierThan: earlierThan, success: success, failure: failure)
}
Expand Down
12 changes: 6 additions & 6 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@
8B15CDAC27EB89AD00A75749 /* BlogDashboardPostsParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B15CDAA27EB89AC00A75749 /* BlogDashboardPostsParser.swift */; };
8B15D27428009EBF0076628A /* BlogDashboardAnalytics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B15D27328009EBF0076628A /* BlogDashboardAnalytics.swift */; };
8B15D27528009EBF0076628A /* BlogDashboardAnalytics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B15D27328009EBF0076628A /* BlogDashboardAnalytics.swift */; };
8B16CE9A25251C89007BE5A9 /* ReaderPostService+PostsV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B16CE9925251C89007BE5A9 /* ReaderPostService+PostsV2.swift */; };
8B16CE9A25251C89007BE5A9 /* ReaderPostStreamService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B16CE9925251C89007BE5A9 /* ReaderPostStreamService.swift */; };
8B1CF00F2433902700578582 /* PasswordAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B1CF00E2433902700578582 /* PasswordAlertController.swift */; };
8B1CF0112433E61C00578582 /* AbstractPost+TitleForVisibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B1CF0102433E61C00578582 /* AbstractPost+TitleForVisibility.swift */; };
8B1E62D625758AAF009A0F80 /* ActivityTypeSelectorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B1E62D525758AAF009A0F80 /* ActivityTypeSelectorViewController.swift */; };
Expand Down Expand Up @@ -5144,7 +5144,7 @@
FABB26182602FC2C00C8785C /* RegisterDomainDetailsViewModel+SectionDefinitions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 436D56102117312700CEAA33 /* RegisterDomainDetailsViewModel+SectionDefinitions.swift */; };
FABB26192602FC2C00C8785C /* ActionRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = F53FF3A723EA723D001AD596 /* ActionRow.swift */; };
FABB261A2602FC2C00C8785C /* AppSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA162301CB7031A00E2E110 /* AppSettingsViewController.swift */; };
FABB261B2602FC2C00C8785C /* ReaderPostService+PostsV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B16CE9925251C89007BE5A9 /* ReaderPostService+PostsV2.swift */; };
FABB261B2602FC2C00C8785C /* ReaderPostStreamService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B16CE9925251C89007BE5A9 /* ReaderPostStreamService.swift */; };
FABB261C2602FC2C00C8785C /* ReferrerStatsRecordValue+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 400A2C7D2217A985000A8A59 /* ReferrerStatsRecordValue+CoreDataProperties.swift */; };
FABB261E2602FC2C00C8785C /* PostCardCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57AA848E228715DA00D3C2A2 /* PostCardCell.swift */; };
FABB26202602FC2C00C8785C /* iAd.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7E21C760202BBC8D00837CF5 /* iAd.framework */; };
Expand Down Expand Up @@ -7074,7 +7074,7 @@
8B0CE7D22481CFF8004C4799 /* ReaderDetailHeaderView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ReaderDetailHeaderView.xib; sourceTree = "<group>"; };
8B15CDAA27EB89AC00A75749 /* BlogDashboardPostsParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDashboardPostsParser.swift; sourceTree = "<group>"; };
8B15D27328009EBF0076628A /* BlogDashboardAnalytics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDashboardAnalytics.swift; sourceTree = "<group>"; };
8B16CE9925251C89007BE5A9 /* ReaderPostService+PostsV2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ReaderPostService+PostsV2.swift"; sourceTree = "<group>"; };
8B16CE9925251C89007BE5A9 /* ReaderPostStreamService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderPostStreamService.swift; sourceTree = "<group>"; };
8B1CF00E2433902700578582 /* PasswordAlertController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasswordAlertController.swift; sourceTree = "<group>"; };
8B1CF0102433E61C00578582 /* AbstractPost+TitleForVisibility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AbstractPost+TitleForVisibility.swift"; sourceTree = "<group>"; };
8B1E62D525758AAF009A0F80 /* ActivityTypeSelectorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityTypeSelectorViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -13413,7 +13413,7 @@
8BB185C524B5FB8500A4CCE8 /* ReaderCardService.swift */,
5D3D559518F88C3500782892 /* ReaderPostService.h */,
5D3D559618F88C3500782892 /* ReaderPostService.m */,
8B16CE9925251C89007BE5A9 /* ReaderPostService+PostsV2.swift */,
8B16CE9925251C89007BE5A9 /* ReaderPostStreamService.swift */,
FA8E1F7625EEFA7300063673 /* ReaderPostService+RelatedPosts.swift */,
E62079E01CF7A61200F5CD46 /* ReaderSearchSuggestionService.swift */,
17CE77EC20C6C2F3001DEA5A /* ReaderSiteSearchService.swift */,
Expand Down Expand Up @@ -21730,7 +21730,7 @@
F53FF3A823EA723D001AD596 /* ActionRow.swift in Sources */,
FFA162311CB7031A00E2E110 /* AppSettingsViewController.swift in Sources */,
F111B87826580FCE00057942 /* BloggingRemindersStore.swift in Sources */,
8B16CE9A25251C89007BE5A9 /* ReaderPostService+PostsV2.swift in Sources */,
8B16CE9A25251C89007BE5A9 /* ReaderPostStreamService.swift in Sources */,
400A2C872217A985000A8A59 /* ReferrerStatsRecordValue+CoreDataProperties.swift in Sources */,
57AA848F228715DA00D3C2A2 /* PostCardCell.swift in Sources */,
FE43DAAF26DFAD1C00CFF595 /* CommentContentTableViewCell.swift in Sources */,
Expand Down Expand Up @@ -24457,7 +24457,7 @@
9815D0B426B49A0600DF7226 /* Comment+CoreDataProperties.swift in Sources */,
FABB261A2602FC2C00C8785C /* AppSettingsViewController.swift in Sources */,
4A82C43228D321A300486CFF /* Blog+Post.swift in Sources */,
FABB261B2602FC2C00C8785C /* ReaderPostService+PostsV2.swift in Sources */,
FABB261B2602FC2C00C8785C /* ReaderPostStreamService.swift in Sources */,
FABB261C2602FC2C00C8785C /* ReferrerStatsRecordValue+CoreDataProperties.swift in Sources */,
FABB261E2602FC2C00C8785C /* PostCardCell.swift in Sources */,
8F228B22E190FF92D05E53DB /* TimeZoneSearchHeaderView.swift in Sources */,
Expand Down