中断正在进行的NSURLConnection

时间:2014-05-22 09:59:25

标签: ios

我在github here上有一个UIProgressView自定义类(UIDownloadbar),我试图在此类中添加一个功能来中断所有下载。因此我添加了这种类型的共享实例:

+ (UIDownloadBar *)sharedInstance {
    static UIDownloadBar *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[UIDownloadBar alloc] init];
    });
    return sharedInstance;
}

+(void)forceStopForIdentifier:(NSString *)identifier{
    UIDownloadBar *storeManager = [UIDownloadBar sharedInstance];
    [storeManager forceStopForIdentifier:identifier];
}

- (void)forceStopForIdentifier:(NSString *)identifier{
    operationBreaked = YES;
    stopString = @"stop";
}

代码工作正常,stopString值已更改为停止。但是它似乎没有改变下载方法中的数据:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"value of string: %@", stopString);
    if (![stopString isEqualToString:@"stop"]) {

        [self.receivedData appendData:data];

    float receivedLen = [data length];
    bytesReceived = (bytesReceived + receivedLen);

    if(expectedBytes != NSURLResponseUnknownLength) {
        self.progress = ((bytesReceived/(float)expectedBytes)*100)/100;
        percentComplete = self.progress*100;
    }
    //NSLog(@" Data receiving... Percent complete: %f", percentComplete);

    [delegate downloadBarUpdated:self];

    } else {
        [connection cancel];
        NSLog(@" STOP !!!!  Receiving data was stoped");
    }

}

根据NSLog我用来识别变化的字符串(或BOOL或其他)的值,不会改变。它固定为原始值,即使已更改

这是否有原因?

0 个答案:

没有答案