iOS 4.0 后台任务完成

发布于 2024-09-12 14:33:40 字数 1603 浏览 3 评论 0原文

使用 iOS 4.0,我尝试在应用程序暂停时在后台下载图像。我正在做的是,当我收到 applicationDidEnterBackground 委托调用时,我启动一个异步 NSURLConnection 并将应用程序委托设置为连接的委托。但没有任何 NSURLConnection 委托被回调。我已经使用 Wireshark 捕获了网络调用,我可以看到我的请求成功并得到了响应。但由于没有调用任何委托方法,我无法对数据执行任何操作。

- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
// UIBackgroundTaskIdentifier bgTask is instance variable
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;
    });
}];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://animal.discovery.com/mammals/leopard/pictures/leopard-picture.jpg"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[self.connection start];
[request release];
NSLog(@"Sent download request....");
dispatch_async(dispatch_get_main_queue(), ^{
    while([application backgroundTimeRemaining] > 1.0) {
        //Do something..
    }
    [application endBackgroundTask:self->bgTask];
    self->bgTask = UIBackgroundTaskInvalid;
    NSLog(@"Ending background task....");
});
}

当应用程序进入后台时,我应该怎么做才能在后台异步完成下载?

Using iOS 4.0 I am trying to do a download an image in the background when the app is suspended. What I am doing is when I get the applicationDidEnterBackground delegate call, I initiate one asynchronous NSURLConnection and set the app delegate as the delegate for the connection. But none of the NSURLConnection delegates are getting called back. I have captured the network calls using Wireshark and I can see that my request succeeded and got the response too. But since none of the delegate methods are invoked I am unable to do anything with the data.

- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
// UIBackgroundTaskIdentifier bgTask is instance variable
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;
    });
}];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://animal.discovery.com/mammals/leopard/pictures/leopard-picture.jpg"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[self.connection start];
[request release];
NSLog(@"Sent download request....");
dispatch_async(dispatch_get_main_queue(), ^{
    while([application backgroundTimeRemaining] > 1.0) {
        //Do something..
    }
    [application endBackgroundTask:self->bgTask];
    self->bgTask = UIBackgroundTaskInvalid;
    NSLog(@"Ending background task....");
});
}

What should I do to complete a download asynchronously in background when the application goes to background?

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

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

发布评论

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

评论(1

最近可好 2024-09-19 14:33:40

我认为这是因为您在下载实际完成之前结束了后台任务。下载完成(成功和失败)时,您应该在 NSURLConnectionDelegate 方法中调用 [application endBackgroundTask:self->bgTask];,而不是在调用 [self.连接开始]

I think it's because you're ending the background task before the download actually finishes. You should put the call to [application endBackgroundTask:self->bgTask]; in the NSURLConnectionDelegate method when the download finishes (both success and unsuccessfully), and not just after calling [self.connection start]

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