【问题标题】:App Crash on slow internet connections网速慢时应用程序崩溃
【发布时间】:2013-01-17 07:03:05
【问题描述】:

每当出现连接问题、2G 连接速度慢等情况时,我的应用都会崩溃并显示以下日志:

我可以从日志中得到的是,它在 NSURLConnectionsendSynchronousRequest 方法上崩溃。我怎么知道到底是什么问题,我该如何解决? 我使用了 Apple 提供的 Reachability 方法,但将 YES 返回到 Internet 可访问性和主机可访问性。只是互联网连接速度很慢。 在快速连接 (Wifi) 上,它运行良好。

编辑:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window setFrame:[[UIScreen mainScreen] bounds]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//singleton
u=[[U5 alloc]init];
m_tUSyncPersistableConfig  = [[USyncPersistableConfig alloc] init] ;    
    m_commonObj = [[CommonClass alloc] init] ;
u.m_tUSyncPersistableConfig=m_tUSyncPersistableConfig;
    u.commonObj = m_commonObj;


 //register for push notifications
   [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

 //load persisting data : from sqlite database
[u loadPreferences:m_tUSyncPersistableConfig];


window.rootViewController = tabBarController;

[window makeKeyAndVisible];


  if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {
       //first launch//setting some values
  }else {
        //not first launch
}

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"] || [u.m_tUSyncPersistableConfig.mUserName isEqualToString:@""] || !u.m_tUSyncPersistableConfig.mUserName)
{
    // This is the first launch ever
    //present login page

}
else
{
    // app already launched
  [[u commonObj] performSelectorInBackground:@selector(getAccountInfo) withObject:nil];
}

return YES;
}

【问题讨论】:

  • 你检查过这个吗:-stackoverflow.com/questions/10288133/…
  • 这并不能解决我的问题。我在后台线程中调用所有连接方法,在主线程中没有
  • 这似乎不是真的。堆栈跟踪显示您正在使用NSURLConnection 在主线程上发送同步请求。你确定你没有错过任何一个?
  • @NikitaP 当你从网络上得到响应时,你为什么不做这项工作,在viewDidLoad 中设置一个加载指示器或其他东西?

标签: iphone ios crash reachability internet-connection


【解决方案1】:

我强烈建议不要使用同步 NSURLConnection 网络请求。 Apple 不推荐它,并且被认为是糟糕的设计。我建议转向异步请求——它可能会回避您的问题,并且您可以使用 NSURLConnection 委托方法处理错误。

【讨论】:

  • 我正在做的是在后台线程中调用一个方法,该方法发送同步请求,因为我当时需要响应。如果我使用异步 API,我将不知道何时收到响应,整个设计将崩溃。不是吗?
【解决方案2】:

在后台线程中运行同步请求通常没问题。

但是崩溃报告显示同步请求正在主线程上运行。因此,至少有一个位置您没有在后台线程中运行它。在主线程上,它会阻塞 UI,iOS 看门狗进程会注意到这一点并在启动时终止应用。

所以请确保您永远不会在主线程上使用同步请求!

您是说您正在这样做,但也许您做错了。显示实际调用连接方法的代码。如果您对崩溃报告进行符号化,它还将在线程 0 堆栈跟踪的第 8 帧到第 10 帧中显示这些位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多