diff options
-rw-r--r-- | macosx/HBController.m | 2 | ||||
-rw-r--r-- | macosx/HBQueue.m | 6 | ||||
-rw-r--r-- | macosx/HBQueueController.h | 2 | ||||
-rw-r--r-- | macosx/HBQueueTableViewController.m | 2 | ||||
-rw-r--r-- | macosx/HBRemoteCore.h | 1 | ||||
-rw-r--r-- | macosx/HBRemoteCore.m | 72 |
6 files changed, 63 insertions, 22 deletions
diff --git a/macosx/HBController.m b/macosx/HBController.m index 8f45ca51f..f10d771f3 100644 --- a/macosx/HBController.m +++ b/macosx/HBController.m @@ -128,7 +128,7 @@ static void *HBControllerScanCoreContext = &HBControllerScanCoreContext; @property (nonatomic, strong) HBAppDelegate *delegate; /// The queue. -@property (nonatomic, strong) HBQueue *queue; +@property (nonatomic, weak) HBQueue *queue; /// Whether the window is visible or occluded, /// useful to avoid updating the UI needlessly diff --git a/macosx/HBQueue.m b/macosx/HBQueue.m index 7e285c185..d85c662d4 100644 --- a/macosx/HBQueue.m +++ b/macosx/HBQueue.m @@ -94,6 +94,12 @@ NSString * const HBQueueItemNotificationItemKey = @"HBQueueItemNotificationItemK } } +- (void)dealloc +{ + [self.core removeObserver:self forKeyPath:@"state"]; + [self.core invalidate]; +} + #pragma mark - Public methods - (void)addJob:(HBJob *)item diff --git a/macosx/HBQueueController.h b/macosx/HBQueueController.h index 722416cd1..246a267f6 100644 --- a/macosx/HBQueueController.h +++ b/macosx/HBQueueController.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithQueue:(HBQueue *)queue; -@property (nonatomic, readonly) HBQueue *queue; +@property (nonatomic, weak, readonly) HBQueue *queue; @property (nonatomic, weak, nullable) HBAppDelegate *delegate; diff --git a/macosx/HBQueueTableViewController.m b/macosx/HBQueueTableViewController.m index 80521d165..2f3931c8e 100644 --- a/macosx/HBQueueTableViewController.m +++ b/macosx/HBQueueTableViewController.m @@ -14,7 +14,7 @@ @interface HBQueueTableViewController () <NSTableViewDataSource, NSTableViewDelegate, HBQueueItemViewDelegate> -@property (nonatomic, readonly) HBQueue *queue; +@property (nonatomic, weak, readonly) HBQueue *queue; @property (nonatomic) NSArray<HBQueueItem *> *dragNodesArray; @property (strong) id<HBQueueTableViewControllerDelegate> delegate; diff --git a/macosx/HBRemoteCore.h b/macosx/HBRemoteCore.h index 6bd43c141..0193743f6 100644 --- a/macosx/HBRemoteCore.h +++ b/macosx/HBRemoteCore.h @@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @interface HBRemoteCore : NSObject - (instancetype)initWithLogLevel:(NSInteger)level name:(NSString *)name; +- (void)invalidate; @property (nonatomic, readonly) HBState state; diff --git a/macosx/HBRemoteCore.m b/macosx/HBRemoteCore.m index b3db5b37b..7ed606ae2 100644 --- a/macosx/HBRemoteCore.m +++ b/macosx/HBRemoteCore.m @@ -14,6 +14,8 @@ @property (nonatomic, readonly) id<HBRemoteCoreProtocol> proxy; @property (nonatomic, readwrite) HBState state; +@property (nonatomic, readonly) NSInteger level; +@property (nonatomic, readonly, copy) NSString *name; @property (nonatomic, readwrite, copy) HBCoreProgressHandler progressHandler; @property (nonatomic, readwrite, copy) HBCoreCompletionHandler completionHandler; @@ -44,29 +46,54 @@ _connection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(HBRemoteProgressProtocol)]; _connection.exportedObject = self; - _proxy = [_connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { - - }]; + __weak HBRemoteCore *weakSelf = self; + + _connection.interruptionHandler = ^{ + dispatch_sync(dispatch_get_main_queue(), ^{ + [weakSelf handleInterruption]; + }); + }; + + _proxy = [_connection remoteObjectProxy]; [_connection resume]; } + +- (void)invalidate +{ + [[_connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {}] tearDown]; + [_connection invalidate]; + _connection = nil; +} + +- (void)handleInterruption +{ + if (self.state != HBStateIdle) + { + self.progressHandler = nil; + if (self.completionHandler) + { + self.completionHandler(HBCoreResultFailed); + } + self.completionHandler = nil; + self.state = HBStateIdle; + } + [_proxy setUpWithLogLevel:self.level name:self.name]; +} + - (instancetype)initWithLogLevel:(NSInteger)level name:(NSString *)name { self = [self init]; if (self) { + _level = level; + _name = name; [_proxy setUpWithLogLevel:level name:name]; } return self; } -- (void)dealloc -{ - [_connection.remoteObjectProxy tearDown]; - [_connection invalidate]; -} - - (void)updateState:(HBState)state { dispatch_sync(dispatch_get_main_queue(), ^{ self.state = state; @@ -94,14 +121,16 @@ self.completionHandler = completionHandler; NSData *bookmark = [url bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:NULL]; - [_connection.remoteObjectProxy provideResourceAccessWithBookmarks:@[bookmark]]; + [_proxy provideResourceAccessWithBookmarks:@[bookmark]]; self.state = HBStateScanning; - [_connection.remoteObjectProxy scanURL:url titleIndex:index previews:previewsNum minDuration:seconds withReply:^(HBCoreResult result) { + __weak HBRemoteCore *weakSelf = self; + + [_proxy scanURL:url titleIndex:index previews:previewsNum minDuration:seconds withReply:^(HBCoreResult result) { dispatch_sync(dispatch_get_main_queue(), ^{ - self.progressHandler = nil; - self.completionHandler(result); + weakSelf.progressHandler = nil; + weakSelf.completionHandler(result); }); }]; } @@ -114,17 +143,19 @@ - (void)encodeJob:(HBJob *)job progressHandler:(HBCoreProgressHandler)progressHandler completionHandler:(HBCoreCompletionHandler)completionHandler { NSData *bookmark = [job.outputURL bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:NULL]; - [_connection.remoteObjectProxy provideResourceAccessWithBookmarks:@[bookmark]]; + [_proxy provideResourceAccessWithBookmarks:@[bookmark]]; self.progressHandler = progressHandler; self.completionHandler = completionHandler; self.state = HBStateWorking; - [_connection.remoteObjectProxy encodeJob:job withReply:^(HBCoreResult result) { + __weak HBRemoteCore *weakSelf = self; + + [_proxy encodeJob:job withReply:^(HBCoreResult result) { dispatch_sync(dispatch_get_main_queue(), ^{ - self.progressHandler = nil; - self.completionHandler(result); + weakSelf.progressHandler = nil; + weakSelf.completionHandler(result); }); }]; } @@ -135,10 +166,13 @@ } - (void)updateProgress:(double)currentProgress hours:(int)hours minutes:(int)minutes seconds:(int)seconds state:(HBState)state info:(NSString *)info { + + __weak HBRemoteCore *weakSelf = self; + dispatch_sync(dispatch_get_main_queue(), ^{ HBProgress progress = {currentProgress , hours, minutes, seconds}; - self.state = state; - self.progressHandler(state, progress, info); + weakSelf.state = state; + weakSelf.progressHandler(state, progress, info); }); } |