Ignore:
Timestamp:
Mar 10, 2012, 1:59:44 PM (10 years ago)
Author:
livings124
Message:

replace a method with GCD calls

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/BlocklistDownloader.m

    r13248 r13250  
    3232- (void) startDownload;
    3333- (void) decompressBlocklist;
    34 - (void) finishDownloadSuccess;
    3534
    3635@end
     
    3837@implementation BlocklistDownloader
    3938
    40 BlocklistDownloader * fDownloader = nil;
     39BlocklistDownloader * fBLDownloader = nil;
    4140+ (BlocklistDownloader *) downloader
    4241{
    43     if (!fDownloader)
     42    if (!fBLDownloader)
    4443    {
    45         fDownloader = [[BlocklistDownloader alloc] init];
    46         [fDownloader startDownload];
     44        fBLDownloader = [[BlocklistDownloader alloc] init];
     45        [fBLDownloader startDownload];
    4746    }
    4847   
    49     return fDownloader;
     48    return fBLDownloader;
    5049}
    5150
    5251+ (BOOL) isRunning
    5352{
    54     return fDownloader != nil;
     53    return fBLDownloader != nil;
    5554}
    5655
     
    8483    [[BlocklistScheduler scheduler] updateSchedule];
    8584   
    86     fDownloader = nil;
     85    fBLDownloader = nil;
    8786}
    8887
     
    121120    [[BlocklistScheduler scheduler] updateSchedule];
    122121   
    123     fDownloader = nil;
     122    fBLDownloader = nil;
    124123}
    125124
     
    127126{
    128127    fState = BLOCKLIST_DL_PROCESSING;
    129     [self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
     128   
     129    [fViewController setStatusProcessing];
     130   
     131    //process data
     132    NSAssert(fDestination != nil, @"the blocklist file destination has not been specified");
     133   
     134    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     135        [self decompressBlocklist];
     136       
     137        dispatch_async(dispatch_get_main_queue(), ^{
     138            const int count = tr_blocklistSetContent([PrefsController handle], [fDestination UTF8String]);
     139           
     140            //delete downloaded file
     141            [[NSFileManager defaultManager] removeItemAtPath: fDestination error: NULL];
     142           
     143            if (count > 0)
     144                [fViewController setFinished];
     145            else
     146                [fViewController setFailed: NSLocalizedString(@"The specified blocklist file did not contain any valid rules.",
     147                                                              "blocklist fail message")];
     148           
     149            //update last updated date for schedule
     150            NSDate * date = [NSDate date];
     151            [[NSUserDefaults standardUserDefaults] setObject: date forKey: @"BlocklistNewLastUpdate"];
     152            [[NSUserDefaults standardUserDefaults] setObject: date forKey: @"BlocklistNewLastUpdateSuccess"];
     153            [[BlocklistScheduler scheduler] updateSchedule];
     154           
     155            [[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
     156           
     157            fBLDownloader = nil;
     158        });
     159    });
    130160}
    131161
     
    222252}
    223253
    224 
    225 - (void) finishDownloadSuccess
    226 {
    227     @autoreleasepool
    228     {
    229         [fViewController setStatusProcessing];
    230        
    231         //process data
    232         NSAssert(fDestination != nil, @"the blocklist file destination has not been specified");
    233        
    234         [self decompressBlocklist];
    235        
    236         const int count = tr_blocklistSetContent([PrefsController handle], [fDestination UTF8String]);
    237        
    238         //delete downloaded file
    239         [[NSFileManager defaultManager] removeItemAtPath: fDestination error: NULL];
    240        
    241         if (count > 0)
    242             [fViewController setFinished];
    243         else
    244             [fViewController setFailed: NSLocalizedString(@"The specified blocklist file did not contain any valid rules.",
    245                                             "blocklist fail message")];
    246        
    247         //update last updated date for schedule
    248         NSDate * date = [NSDate date];
    249         [[NSUserDefaults standardUserDefaults] setObject: date forKey: @"BlocklistNewLastUpdate"];
    250         [[NSUserDefaults standardUserDefaults] setObject: date forKey: @"BlocklistNewLastUpdateSuccess"];
    251         [[BlocklistScheduler scheduler] updateSchedule];
    252        
    253         [[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
    254     }
    255    
    256     fDownloader = nil;
    257 }
    258 
    259254@end
Note: See TracChangeset for help on using the changeset viewer.