当网络连接不佳时,iOS 应用程序在启动时会挂起一段时间
<p><p>我有一个大问题,我正在地铁中测试我的应用程序,当我在一个 super 糟糕的覆盖站中时,我的应用程序在启动应用程序的第一个屏幕上卡住了一段时间(如 30 秒或 1 分钟) cero,然后一切都开始像魅力一样工作。</p>
<p>我有 2 个函数负责在我的 WS 中查找信息(响应是 JSON),并且我已将此 NSURLRequest 的时间设置为 2.5 秒,所以如果服务器有点慢我为什么要查找存储在 iPhone 中而不是服务器中的 JSON 文件。</p>
<p>如果我在飞行模式下测试我的应用程序,我的代码就像一个魅力,一切都从设备中读取,但如果我的覆盖率非常低或者我模拟了它,应用程序卡住,然后像在飞机上一样工作模式。</p>
<p>如果我在 Xcode 中添加断点,一切都会像往常一样执行(我的意思是快速),最后调用的函数是 viewWillAppear:.</p>
<p>有没有人有类似的问题?或者有人知道会发生什么吗?</p>
<p>任何想法将不胜感激。</p>
<p>提前致谢。</p>
<p>这是我用来获取 JSON 的函数:</p>
<pre><code>-(NSData *)getMainMenuJsonData{
NSUserDefaults *defaults = ;
if ([ boolValue] ) {
NSString *urlAsString = , , , .getAppLanguage];
NSURLRequest * urlRequest = cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2.5];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = ;
plistPath = [ pathForResource:@"Config" ofType:@"plist"];
configs = [ initWithContentsOfFile:plistPath];
NSUInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
if (statusCode == 200 && error == Nil) {
if (error != Nil) {
return Nil;
} else {
return data;
}
}
else {
return Nil;
}
}
else {
return Nil;
}
}
-(NSData *)getSpecificJsonData:(NSString *)itemId{
NSString *urlAsString = , , , .getAppLanguage, itemId];
NSURLRequest * urlRequest = cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2.5];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = ;
plistPath = [ pathForResource:@"Config" ofType:@"plist"];
configs = [ initWithContentsOfFile:plistPath];
NSUInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
if (statusCode == 200 && error == Nil) {
if (error != Nil) {
return Nil;
} else {
return data;
}
}
else {
return Nil;
}
}
</code></pre>
<p>这是我主屏幕的所有代码,一旦加载并调用 viewWillAppear:,如果我触摸屏幕上的一个按钮,执行 btnAction: 函数需要一段时间(如 30 秒或 1 分钟) :</p>
<pre><code>//
//vcMainScreen.m
//SmartHotel
//
//Created by GoSmart on 08/07/13.
//Copyright (c) 2013 GoSmart. All rights reserved.
//
#import "vcMainScreen.h"
#import "Util.h"
#import "Reachability.h"
#import "CommonsUtils.h"
@interface vcMainScreen ()
@property (nonatomic) Reachability *reachabilityInfo;
@end
@implementation vcMainScreen {
NSDictionary *dValue;
NSData *jsonData;
NSDictionary *dConfiguration, *configs, *languages;
Util *util;
BOOL ok;
NSString *plistPath, *language;
}
@synthesize tabController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = ;
if (self) {
}
return self;
}
- (void)viewDidLoad
{
_reachabilityInfo = ;
;
;
util = [ crearUtil];
[ setNavigationBarHidden:YES animated:NO];
NetworkStatus internetStatus = ;
NSData *mainData = Nil;
if (internetStatus != NotReachable)
mainData = ;
if (mainData != Nil) {
jsonData = mainData;
}
else {
plistPath = [ pathForResource:@"Config" ofType:@"plist"];
configs = [ initWithContentsOfFile:plistPath];
languages = ;
for (NSString * appLanguage in )
{
language = [ objectAtIndex:0];
if (]) {
language = [ objectAtIndex:0];
}
else{
break;
}
}
NSString *jsonPath = ,language];
jsonData = ;
}
dConfiguration =;
;
NSInteger count = 0;
for (UIView* subView in self.view.subviews)
{
if (]) {
UIButton *bCustom = (UIButton *)subView;
;
count ++;
}
}
dValue = [ objectAtIndex:0];
_ivBackGround.image = andRetrina:]];
self.navigationItem.title = @"Home";
}
- (void)viewWillAppear:(BOOL)animated
{
;
;
// Tracking view for analytics
id tracker = [ defaultTracker];
;
build]];
}
- (void)viewWillDisappear:(BOOL)animated
{
;
;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([ userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationMaskPortrait;
} else {
return UIInterfaceOrientationMaskAll;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (void)didReceiveMemoryWarning
{
;
}
- (void)createTabBar
{
tabController = ;
dValue = ;
NSMutableArray*aControllers = [ init];
int i = 0;
for (NSString* sProperty in dValue) {
NSString* d = @"Details";
NetworkStatus internetStatus = ;
NSData *itemData = Nil;
NSUserDefaults *defaults = ;
if (internetStatus != NotReachable && [ boolValue])
itemData = ];
if(itemData != nil){
UIStoryboard *aStoryboard = ];
UIViewController *vcCustom = ]];
;
forKey:@"Item"];
;
,.getAppLanguage]];
[ setNavigationBarHidden:NO animated:NO];
vcCustom.navigationItem.leftBarButtonItem = Nil;
vcCustom.navigationItem.hidesBackButton = YES;
UIImage *imageBtn = andRetrina:]];
UIImage *imageBtnPress = andRetrina:]];
UITabBarItem *tab = [ initWithTitle: image:imageBtn selectedImage:imageBtnPress];
UIImage * iSelected = imageBtnPress;
iSelected = ;
;
tab.tag = i;
if([ isEqualToString:@"Notificaciones"])
tab.badgeValue=;
;
];
UINavigationController *navigationController = [ initWithRootViewController:vcCustom];
navigationController.navigationBar.tintColor = ;
;
i++;
}
else
{
UIStoryboard *aStoryboard = ];
UIViewController *vcCustom = ]];
NSNumber *val = ;
NSString *nextJson = ;
forKey:@"Item"];
;
;
[ setNavigationBarHidden:NO animated:NO];
vcCustom.navigationItem.leftBarButtonItem = Nil;
vcCustom.navigationItem.hidesBackButton = YES;
UIImage *imageBtn = andRetrina:]];
UIImage *imageBtnPress = andRetrina:]];
UITabBarItem *tab = [ initWithTitle: image:imageBtn selectedImage:imageBtnPress];
UIImage * iSelected = imageBtnPress;
iSelected = ;
;
tab.tag = i;
if([ isEqualToString:@"Notificaciones"])
tab.badgeValue=;
;
];
UINavigationController *navigationController = [ initWithRootViewController:vcCustom];
navigationController.navigationBar.tintColor = ;
;
i++;
}
}
tabController.delegate = self;
tabController.viewControllers = aControllers;
tabController.tabBar.tintColor = ;
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
return YES;
}
- (IBAction)btnAction:(id)sender {
UIButton *bCustom = (UIButton *)sender;
tabController.selectedIndex = bCustom.tag;
id<GAITracker> tracker= [ defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory: @"ui_button"
action: @"button_press"
label: title]]
value: nil] build]];
;
}
@end
</code></pre>
<p>这是我 iPhone 中的设置:</p>
<p> <img src="/image/0XYnI.png" alt="Network settings"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>对此没有正确答案,由于@Guillaume Algis 和
<a href="https://stackoverflow.com/questions/10196344/my-app-freezes-but-no-error-appears" rel="noreferrer noopener nofollow">this</a>我能够找到并解决我的问题。</p>
<p>一如既往地感谢您的帮助。</p></p>
<p style="font-size: 20px;">关于当网络连接不佳时,iOS 应用程序在启动时会挂起一段时间,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28067080/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28067080/
</a>
</p>
页:
[1]