2013-09-26 123 views
1

我有一个应用程序,其中有一个与我们的SQL数据库同步的例程。我遇到了一些问题,看起来很糟糕的无线接入。可怜的wifi连接导致iPad应用程序崩溃

的日常工作是这样的:

1 - 按下的UIButton

2 - 因特网连接进行检查,如果有连接....

3 - 一个新的线程开始显示'Loading'动画gif

4 - 下一页被加载。

if([self connectedToInternet] == YES) 
{ 
[NSThread detachNewThreadSelector:@selector(loadAnimation) toTarget:self withObject:nil]; 

ObViewControllerAdminMenu *monitorMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"webObservations"]; 
monitorMenuViewController.modalTransitionStyle = IModalTransitionStyleCrossDissolve; 
[self presentViewController:monitorMenuViewController animated:YES completion:nil]; 
} 

然后,使用webObservations页面中的viewDidLoad方法,我开始连接到SQL数据库。

我的问题是如果在同步过程中互联网连接丢失会发生什么?在我看来,应用程序'超时',并由于缺乏反应,它关闭了自己。

我认为我说的是,iPad在5秒钟没有活动之后会这样做 - 这是正确的吗?如果是的话,它的方式是什么?

的同步代码片段是下面是否有帮助:

- (void)viewDidLoad 
{ 
NSString *strURLClass = [NSString stringWithFormat:@"%@%@", @"http://www.website.co.uk/uploads/getiobserveinfo.php?schoolname=",obsSchoolName]; 
NSArray *observationsArrayClass = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]]; 
NSEnumerator *enumForObsClass = [observationsArrayClass objectEnumerator]; 

observationListFromSQL = [[NSMutableArray alloc]init]; 

id className, dateOfObs, teacher, startTime; 

while (className = [enumForObsClass nextObject]) 
{ 
[observationListFromSQL addObject:[NSDictionary dictionaryWithObjectsAndKeys:className, @"obsClassName", nil]]; 
} 

从崩溃日志 - 请注意,它运行在推出的程序也

日期/时间:2013年9月20日11:56:31.731 0300 OS版本:iOS的6.1.3(10B329) 报告版本:104

异常类型:00000020 异常代码:0x000000008badf00d 突出螺纹:0

特定应用信息: uk.co.website未能及时启动

经过的总CPU时间(秒):2.080(用户2.080,0.000系统),5%CPU 消逝应用CPU时间(秒):0.312,1%的CPU

+0

嗯,我不知道Objective-C,但它听起来像异常处理应该解决(至少它不会崩溃,但能够通过“重试”按钮来处理它) – Najzero

+0

@Richard请添加崩溃日志。 – Amar

+0

@Amar我添加了一块崩溃日志。我认为这表示暂停。 – RGriffiths

回答

1

你的错误是:应用程序的具体信息:uk.co.website未能及时推出

请避免使用同步功能来获取网络资源,只有initWithContentsOfURL会从网络下载完整的网址时返回。 此方法调用将阻止应用程序启动。

有异步方法来下载资源如NSURLConnection的或使用第三方库像AFNetworking

1

错误代码0x000000008badf00d意味着你的应用程序是没有得到坠毁而是通过iOS的,因为你的应用程序UI冻结杀害,并花了太久无法回应。我怀疑这行是罪魁祸首,

NSArray *observationsArrayClass = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]]; 

这是越来越称为主线程从而使UI冻结直到发生看门狗超时和你的应用程序就会被杀死的同步方法。

解决方案

进行异步调用WS使主线程没有被阻塞。你可以使用GCD来实现这一点。

- (void)doAsyncCall 
{ 
    //you can use any string instead "com.mycompany.myqueue" 
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0); 

    dispatch_async(backgroundQueue, ^{ 
     //Make WS call here. 
     //Parse response and create datasource for your UI elements 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Pass the datasource to the UI and update. 
     });  
    }); 
} 

希望有所帮助!

+1

非常有帮助的答案 - 谢谢。我会研究这一点,找出哪些地方会让你知道。谢谢你的时间。 – RGriffiths