From 3a8e3b70ebf3127e274653abe30736a318e044ae Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 3 May 2015 17:01:53 -0700 Subject: [PATCH] weakify self in view controllers --- Blog/EditorViewController.m | 7 +++++++ Blog/PostsViewController.m | 4 ++++ Blog/PreviewViewController.m | 3 +++ 3 files changed, 14 insertions(+) diff --git a/Blog/EditorViewController.m b/Blog/EditorViewController.m index 4535e92..b30da84 100644 --- a/Blog/EditorViewController.m +++ b/Blog/EditorViewController.m @@ -277,7 +277,9 @@ [items replaceObjectAtIndex:[items indexOfObject:saveItem] withObject:indicatorItem]; [self.toolbar setItems:items animated:NO]; + __weak __typeof__(self) welf = self; return savePromise.then(^{ + __typeof__(self) self = welf; NSLog(@"%@ post at path %@", verb, path); // update our post because "new" may have changed, which is essential to correct operation @@ -290,6 +292,7 @@ NSLog(@"Failed to %@ post at path %@: %@ %@", verb, path, error.localizedDescription, error.userInfo); return error; }).finally(^{ + __typeof__(self) self = welf; self.textView.editable = YES; self.savePromise = nil; [items replaceObjectAtIndex:[items indexOfObject:indicatorItem] withObject:saveItem]; @@ -314,7 +317,9 @@ - (IBAction)publishOrUnpublish:(id)sender { // TODO: prevent changes while publishing + __weak __typeof__(self) welf = self; [self savePost].then(^{ + __typeof__(self) self = welf; PMKPromise *promise = nil; Post *post = self.modifiedPost; if (post.draft) { @@ -395,7 +400,9 @@ NSURL *currentURL = self.modifiedPost.url; if (currentURL) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"TODO" message:@"show a web browser" preferredStyle:UIAlertControllerStyleAlert]; + __weak __typeof__(self) welf = self; [alertController addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + __typeof__(self) self = welf; [self dismissViewControllerAnimated:YES completion:nil]; }]]; [self presentViewController:alertController animated:YES completion:nil]; diff --git a/Blog/PostsViewController.m b/Blog/PostsViewController.m index 9c62b4d..0f27545 100644 --- a/Blog/PostsViewController.m +++ b/Blog/PostsViewController.m @@ -200,7 +200,9 @@ static const NSUInteger SectionPublished = 1; [self requestStatusWithoutCaching]; }).catch(^(NSError *error) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; + __weak __typeof__(self) welf = self; [alertController addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + __typeof__(self) self = welf; [self dismissViewControllerAnimated:YES completion:nil]; }]]; [self presentViewController:alertController animated:YES completion:nil]; @@ -220,7 +222,9 @@ static const NSUInteger SectionPublished = 1; [controller configureWithPost:post]; controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; controller.navigationItem.leftItemsSupplementBackButton = YES; + __weak __typeof__(self) welf = self; controller.postUpdatedBlock = ^(Post *post) { + __typeof__(self) self = welf; BOOL (^isThisPost)(Post *, NSUInteger, BOOL *) = ^BOOL(Post *p, NSUInteger idx, BOOL *stop) { return [p.objectID isEqualToString:post.objectID]; }; diff --git a/Blog/PreviewViewController.m b/Blog/PreviewViewController.m index 497431f..5da7070 100644 --- a/Blog/PreviewViewController.m +++ b/Blog/PreviewViewController.m @@ -21,9 +21,12 @@ if (self.initialRequest) { if (self.promise) { + __weak __typeof__(self) welf = self; self.promise.then(^{ + __typeof__(self) self = welf; [self.webView loadRequest:self.initialRequest]; }).finally(^{ + __typeof__(self) self = welf; self.promise = nil; }); }