Reset SQRLUpdater when setFeedURL is called

This commit is contained in:
Cheng Zhao 2016-06-13 09:38:49 +09:00
parent a3786f66c9
commit 26c4fc34cb

View file

@ -38,34 +38,33 @@ void AutoUpdater::SetFeedURL(const std::string& feed,
NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)]; NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url]; NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
for (auto&& it : requestHeaders) { for (const auto& it : requestHeaders) {
[urlRequest setValue:base::SysUTF8ToNSString(it.second) [urlRequest setValue:base::SysUTF8ToNSString(it.second)
forHTTPHeaderField:base::SysUTF8ToNSString(it.first)]; forHTTPHeaderField:base::SysUTF8ToNSString(it.first)];
} }
if (g_updater == nil) { if (g_updater)
// Initialize the SQRLUpdater. [g_updater release];
@try {
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
} @catch (NSException* error) {
delegate->OnError(base::SysNSStringToUTF8(error.reason));
return;
}
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater] // Initialize the SQRLUpdater.
subscribeNext:^(NSNumber *stateNumber) { @try {
int state = [stateNumber integerValue]; g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
// Dispatching the event on main thread. } @catch (NSException* error) {
dispatch_async(dispatch_get_main_queue(), ^{ delegate->OnError(base::SysNSStringToUTF8(error.reason));
if (state == SQRLUpdaterStateCheckingForUpdate) return;
delegate->OnCheckingForUpdate();
else if (state == SQRLUpdaterStateDownloadingUpdate)
delegate->OnUpdateAvailable();
});
}];
} else {
g_updater.updateRequest = urlRequest;
} }
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
subscribeNext:^(NSNumber *stateNumber) {
int state = [stateNumber integerValue];
// Dispatching the event on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
if (state == SQRLUpdaterStateCheckingForUpdate)
delegate->OnCheckingForUpdate();
else if (state == SQRLUpdaterStateDownloadingUpdate)
delegate->OnUpdateAvailable();
});
}];
} }
// static // static