在iOS4设备上在后台发送大量数据的最佳做法? [英] Best practice to send a lot of data in background on iOS4 device?

查看:150
本文介绍了在iOS4设备上在后台发送大量数据的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要将数据(使用POST)发送到服务器的应用程序。此功能必须位于其中一个NavigationController子控制器上,用户应能够远离此控制器和/或关闭应用程序(仅支持iPhone4 / iOS4)。
我应该使用线程/ NSOperations或/并使用现有的异步方法发送数据吗?
任何想法/最佳实践如何实现这个?

I have an app that needs to send data (using POST) to a server. This function has to be on one of the NavigationController sub-controllers and user should be able to navigate away from this controller and/or close the app (only iPhone4/iOS4 will be supported). Should I use threads/NSOperations or/and send data using existing asynchronous methods? Any ideas/best practices how to implement this?

推荐答案

好的,我会回答我自己的问题。
首先,就像tc所说的那样,最好在应用程序委托上进行此调用,以便可以关闭NavigationController中的View。
其次,用 beginBackgroundTaskWithExpirationHandler:标记后台处理的开始,并以 endBackgroundTask:结束它,如下所示:

OK, I'll answer my own question. First, like tc said, it's better to have this call on the application delegate, so that the View in the NavigationController can be closed. Second, mark beginning of the background processing with beginBackgroundTaskWithExpirationHandler: and end it with endBackgroundTask: like this:

UIBackgroundTaskIdentifier bgTask;

- (void)sendPhoto:(UIImage *)image
{
  UIApplication *app = [UIApplication sharedApplication];

  bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid;
  }];


  NSLog(@"Sending picture...");

  // Init async NSURLConnection

  // ....
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

  NSLog(@"Picture sent.");

  UIApplication *app = [UIApplication sharedApplication];

  if (bgTask != UIBackgroundTaskInvalid) {
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid;
  }
}

你有10分钟的iOS终止你的应用程序。您可以使用 [app backgroundTimeRemaining]

You have 10 minutes before iOS terminates your app. You can check this time with [app backgroundTimeRemaining]

这篇关于在iOS4设备上在后台发送大量数据的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆