在 UIAlertView 中将 ASIHTTPRequest 与 UIProgressView 结合使用

发布于 2024-10-10 13:30:04 字数 2195 浏览 0 评论 0原文

我已经设法将 UIProgressView 添加到 UIAlertView 中/上,并为 ASIHTTPRequest 设置所有内容,但是无论我将 [request startSynchronous]; 放在哪里,它会使应用程序崩溃。

[request startAsynchronous]; 也没有帮助。

谢谢。

这是我到目前为止所拥有的;

有一个检查可以检查文件是否存在等,该检查有效,但为了这个问题我已将其删除。

-(IBAction) update {
    // define everything.
    NSString *script;
    script = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/myfile.txt"];

    NSURL *url = @"http://example.com/myfile.txt";

        UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle: @"Updating..." message: @"Please wait..." delegate: self cancelButtonTitle: nil otherButtonTitles: nil];

        progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];
        [progressAlert addSubview:progressView];
        [progressView setProgressViewStyle: UIProgressViewStyleBar];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDownloadDestinationPath:script];
        [request setDownloadProgressDelegate:progressView];
        [progressAlert show];
        [progressAlert release];
        [request startSynchronous];
        [progressAlert dismissWithClickedButtonIndex:0 animated:YES];
}

调用堆栈;

#0  0x010cda44 in CFURLCopyAbsoluteURL
#1  0x00cee0f4 in HTTPMessage::initialize
#2  0x00cee018 in CFHTTPMessageCreateRequest
#3  0x00016d91 in -[ASIHTTPRequest main] at ASIHTTPRequest.m:855
#4  0x000859a6 in __NSThreadPerformPerform
#5  0x0119a01f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
#6  0x010f828b in __CFRunLoopDoSources0
#7  0x010f7786 in __CFRunLoopRun
#8  0x010f7240 in CFRunLoopRunSpecific
#9  0x010fa504 in CFRunLoopRun
#10 0x0001ae30 in +[ASIHTTPRequest runRequests] at ASIHTTPRequest.m:4286
#11 0x00070d4c in -[NSThread main]
#12 0x00070cd8 in __NSThread__main__
#13 0x9319785d in _pthread_start
#14 0x931976e2 in thread_start

3: request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)[self requestMethod], (CFURLRef)[self url], [self useHTTPVersionOne] ? kCFHTTPVersion1_0 : kCFHTTPVersion1_1);

10: CFRunLoopRun();

I've managed to addSubview: a UIProgressView in/onto a UIAlertView, as well as set everything for ASIHTTPRequest, but where-ever I put the [request startSynchronous];, it crashes the app.

[request startAsynchronous]; doesn't help either.

Thanks.

Here's what I have so far;

There is a check that checks if the file exists etc, which works, but I've removed it for the sake of this question.

-(IBAction) update {
    // define everything.
    NSString *script;
    script = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/myfile.txt"];

    NSURL *url = @"http://example.com/myfile.txt";

        UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle: @"Updating..." message: @"Please wait..." delegate: self cancelButtonTitle: nil otherButtonTitles: nil];

        progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];
        [progressAlert addSubview:progressView];
        [progressView setProgressViewStyle: UIProgressViewStyleBar];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDownloadDestinationPath:script];
        [request setDownloadProgressDelegate:progressView];
        [progressAlert show];
        [progressAlert release];
        [request startSynchronous];
        [progressAlert dismissWithClickedButtonIndex:0 animated:YES];
}

Call stack;

#0  0x010cda44 in CFURLCopyAbsoluteURL
#1  0x00cee0f4 in HTTPMessage::initialize
#2  0x00cee018 in CFHTTPMessageCreateRequest
#3  0x00016d91 in -[ASIHTTPRequest main] at ASIHTTPRequest.m:855
#4  0x000859a6 in __NSThreadPerformPerform
#5  0x0119a01f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
#6  0x010f828b in __CFRunLoopDoSources0
#7  0x010f7786 in __CFRunLoopRun
#8  0x010f7240 in CFRunLoopRunSpecific
#9  0x010fa504 in CFRunLoopRun
#10 0x0001ae30 in +[ASIHTTPRequest runRequests] at ASIHTTPRequest.m:4286
#11 0x00070d4c in -[NSThread main]
#12 0x00070cd8 in __NSThread__main__
#13 0x9319785d in _pthread_start
#14 0x931976e2 in thread_start

3: request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)[self requestMethod], (CFURLRef)[self url], [self useHTTPVersionOne] ? kCFHTTPVersion1_0 : kCFHTTPVersion1_1);

10: CFRunLoopRun();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-10-17 13:30:04

如果没有调用堆栈,很难说出确切的问题是什么。

然而,这段代码可能令人担忧:

    [progressAlert release];
    [request startSynchronous];
    [progressAlert dismissWithClickedButtonIndex:0 animated:YES];

“release”调用应该移到dismissWithClickedButtonIndex 之后。
但我认为这不太可能是事故的原因。

我看到问题现在已经发布了堆栈转储:

NSURL *url = @"http://example.com/myfile.txt";

应该是:

NSURL *url = [NSURL URLWithString:@"http://example.com/myfile.txt"];

说实话,我很惊讶编译器没有就此发出警告。

Without the call stack it's hard to say what the exact problem is.

However this code is potentially worrying:

    [progressAlert release];
    [request startSynchronous];
    [progressAlert dismissWithClickedButtonIndex:0 animated:YES];

The "release" call should be moved to after the dismissWithClickedButtonIndex.
However I think it's unlikely this is the cause of the crash.

I see the problem now've posted the stack dump:

NSURL *url = @"http://example.com/myfile.txt";

should be:

NSURL *url = [NSURL URLWithString:@"http://example.com/myfile.txt"];

To be honest, I'm very surprised the compiler wasn't giving you a warning about that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文