-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor SharingService
to use CoreDataStack
#20265
Changes from all commits
7afedb0
bf99efc
27db21a
89c98fe
102e386
df02800
651530c
2de4175
460d0cb
e95fd66
eaea3de
27c44c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
extension PublicizeService { | ||
/// Finds a cached `PublicizeService` matching the specified service name. | ||
/// | ||
/// - Parameter name: The name of the service. This is the `serviceID` attribute for a `PublicizeService` object. | ||
/// | ||
/// - Returns: The requested `PublicizeService` or nil. | ||
/// | ||
static func lookupPublicizeServiceNamed(_ name: String, in context: NSManagedObjectContext) throws -> PublicizeService? { | ||
let request = NSFetchRequest<PublicizeService>(entityName: PublicizeService.classNameWithoutNamespaces()) | ||
request.predicate = NSPredicate(format: "serviceID = %@", name) | ||
return try context.fetch(request).first | ||
} | ||
|
||
@objc(lookupPublicizeServiceNamed:inContext:) | ||
static func objc_lookupPublicizeServiceNamed(_ name: String, in context: NSManagedObjectContext) -> PublicizeService? { | ||
try? lookupPublicizeServiceNamed(name, in: context) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LOL. I just commented above about the code being better because it doesn't return Of course, this is a special case method, behaving that way to fit with the Objective-C layer 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, a little compromise for Objective-C code. |
||
} | ||
|
||
/// Returns an array of all cached `PublicizeService` objects. | ||
/// | ||
/// - Returns: An array of `PublicizeService`. The array is empty if no objects are cached. | ||
/// | ||
@objc(allPublicizeServicesInContext:error:) | ||
static func allPublicizeServices(in context: NSManagedObjectContext) throws -> [PublicizeService] { | ||
let request = NSFetchRequest<PublicizeService>(entityName: PublicizeService.classNameWithoutNamespaces()) | ||
let sortDescriptor = NSSortDescriptor(key: "order", ascending: true) | ||
request.sortDescriptors = [sortDescriptor] | ||
return try context.fetch(request) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
extension SharingButton { | ||
|
||
/// Returns an array of all cached `SharingButtons` objects. | ||
/// | ||
/// - Returns: An array of `SharingButton`s. The array is empty if no objects are cached. | ||
/// | ||
@objc(allSharingButtonsForBlog:inContext:error:) | ||
static func allSharingButtons(for blog: Blog, in context: NSManagedObjectContext) throws -> [SharingButton] { | ||
let request = NSFetchRequest<SharingButton>(entityName: SharingButton.classNameWithoutNamespaces()) | ||
request.predicate = NSPredicate(format: "blog = %@", blog) | ||
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)] | ||
return try context.fetch(request) | ||
} | ||
|
||
/// Finds a cached `SharingButton` by its `buttonID` for the specified `Blog` | ||
/// | ||
/// - Parameters: | ||
/// - buttonID: The button ID of the `SharingButton`. | ||
/// - blog: The blog that owns the sharing button. | ||
/// | ||
/// - Returns: The requested `SharingButton` or nil. | ||
/// | ||
static func lookupSharingButton(byID buttonID: String, for blog: Blog, in context: NSManagedObjectContext) throws -> SharingButton? { | ||
let request = NSFetchRequest<SharingButton>(entityName: SharingButton.classNameWithoutNamespaces()) | ||
request.predicate = NSPredicate(format: "buttonID = %@ AND blog = %@", buttonID, blog) | ||
return try context.fetch(request).first | ||
} | ||
|
||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,8 +90,7 @@ -(void)presentationControllerDidDismiss:(UIPresentationController *)presentation | |
|
||
- (void)refreshPublicizers | ||
{ | ||
SharingService *sharingService = [[SharingService alloc] initWithManagedObjectContext:[self managedObjectContext]]; | ||
self.publicizeServices = [sharingService allPublicizeServices]; | ||
self.publicizeServices = [PublicizeService allPublicizeServicesInContext:[self managedObjectContext] error:nil]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worth to pass an error pointer here, so that we can log and bail if the method throws? I mean... if the method throws then the data the table should show won't change and this is a no-op, so there's no much waste in not handling the error. So I guess it all boils down to whether we want to capture and log it or not. 🤔 I don't have a strong opinion about this. I'd be tempted to say "log just in case; more information is always better" but... are those logs ever looked at? I know they are sometimes for support, but not sure how much more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I guess capturing the error or not depending on what we are going to do with it. If it's just logging, I would probably say it's not that valuable. Especially in this case, the error (which is Core Data failing to fetch the model objects) is not likely to happen. |
||
|
||
[self.tableView reloadData]; | ||
} | ||
|
@@ -324,7 +323,7 @@ -(void)showConnectionError | |
|
||
- (void)syncPublicizeServices | ||
{ | ||
SharingService *sharingService = [[SharingService alloc] initWithManagedObjectContext:[self managedObjectContext]]; | ||
SharingService *sharingService = [[SharingService alloc] initWithContextManager:[ContextManager sharedInstance]]; | ||
__weak __typeof__(self) weakSelf = self; | ||
[sharingService syncPublicizeServicesForBlog:self.blog success:^{ | ||
[weakSelf syncConnections]; | ||
|
@@ -358,11 +357,12 @@ - (void)syncSharingButtonsIfNeeded | |
{ | ||
// Sync sharing buttons if they have never been synced. Otherwise, the | ||
// management vc can worry about fetching the latest sharing buttons. | ||
SharingService *sharingService = [[SharingService alloc] initWithManagedObjectContext:[self managedObjectContext]]; | ||
NSArray *buttons = [sharingService allSharingButtonsForBlog:self.blog]; | ||
NSArray *buttons = [SharingButton allSharingButtonsForBlog:self.blog inContext:[self managedObjectContext] error:nil]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worth to pass an error pointer here, so that we can log and bail if the method throws? |
||
if ([buttons count] > 0) { | ||
return; | ||
} | ||
|
||
SharingService *sharingService = [[SharingService alloc] initWithContextManager:[ContextManager sharedInstance]]; | ||
[sharingService syncSharingButtonsForBlog:self.blog success:nil failure:^(NSError *error) { | ||
DDLogError([error description]); | ||
}]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I called this out before, but one great benefit of all this work is how much more Swifty the code is getting by leveraging
throws
in many more places as opposed to swallowing errors or return.none
as a sign of error.