gpt4 book ai didi

ios - 同时调用 ViewController 的多个实例导致应用程序在 AppDelegate 中崩溃

转载 作者:行者123 更新时间:2023-11-29 13:21:03 25 4
gpt4 key购买 nike

我在做什么?

  1. 我目前正在为我的应用程序在没有互联网的情况下进行防崩溃。
  2. 我已经安装了 Little snitch 来暂时禁用网络访问。
  3. 发出请求后,如果响应为零,则触发“可达性测试”。以下代码中的 ReachabilityController 只是对 Apple 提供的 Reachability 类的包装。

此代码在其他地方是否有效?

应用程序充满了在线请求,我在其他 View Controller 中从未遇到过这个问题。如果互联网不可用,它会显示一条描述状态的漂亮小消息。

哪里出错了

应用委托(delegate)

错误

* * * -[ReachabilityController respondsToSelector:]: 发送到释放实例 0x12a71320 的消息

ReachabilityController 显示描述错误状态的 UIAlertView。警报 View 显示大约一秒钟,然后应用程序崩溃。

调试和仪器?

NSLog(@"%p", reachability); 记录与错误中相同的地址。 0x12a71320。不用说,这个地址在每次运行时都不同。

Instruments 中的 Zombies 模板也指出了 Reachability Controller。

下图中“Responsible” 部分的 4 行白色补丁包含 App 的名称。

Zombie object

我在期待什么?

老实说,我不知道这种行为背后的原因。我相信它必须是非常基本的。

与现在的本地声明相比,我是否必须声明属性以保留“ReachabilityController”?

代码

发生崩溃的片段。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myUrl"]];
NSData *authResponse = [NSData dataWithContentsOfURL:url];

dispatch_sync(dispatch_get_main_queue(), ^{
if(authResponse == nil) {
ReachabilityController *reachability = [[ReachabilityController alloc] init];
NSLog(@"%p", reachability);
[reachability checkReachability];
NSLog(@"%p", reachability);
return; // app crashes round here
}
}

ReachabilityController.h

#import <UIKit/UIKit.h>
#import "Reachability.h"

@interface ReachabilityController : UIViewController
-(void) checkReachability ;
@end

ReachabilityController.m

#import "ReachabilityController.h"

@interface ReachabilityController ()

@end

@implementation ReachabilityController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[super viewDidUnload];

}


-(void) checkReachability {
Reachability* internetAvailable = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [internetAvailable currentReachabilityStatus];
NSString *messageText;
if (netStatus == NotReachable)
{
messageText = [NSString stringWithFormat:@"iRestaurant has determined that internet access is not available at the moment"];

} else {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *ipAdress = [infoDict objectForKey:@"LookUpIpAdress"];
NSString *host = [NSString stringWithFormat:@"%@/iRestaurant", ipAdress];

Reachability *netReach = [Reachability reachabilityWithHostName:host];
NetworkStatus hostStatus = [netReach currentReachabilityStatus];
if (hostStatus == NotReachable)
{
messageText = [NSString stringWithFormat:@"Sorry, for your inconvenience. iRestaurant server is currently unreachable. The iRestaurant server may be down for maintenance. Please check back after 5 mins"];

} else {
messageText = [NSString stringWithFormat:@"Sorry, for your inconvenience. This service is temporarily unavailable. We are working very hard to get it back on track."];
}

}

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"iRestaurant"
message:messageText
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
[alertView show];

}


@end

更新:

更改问题的标题

只是最近的发现。有 3 个实例从在线服务器请求数据。由于网络不可用,三种方式几乎同时请求Reachability测试。所有这三种方法都有类似于上面的方法声明。这三种方法都使用如下所示的“本地声明”来触发测试。

ReachabilityController *reachability = [ReachabilityController alloc] init];
[reachability checkReachability];

最佳答案

您将 UIAlertView 的委托(delegate)设置为 self。除非您有办法在 UIAlertView 发布之前保留您的 ReachabilityController,否则这是一个悬空引用。

您是否需要 UIAlertView 与您的 ReachabilityController 进行交互?将 nil 作为委托(delegate)传递可能是最简单的。

关于ios - 同时调用 ViewController 的多个实例导致应用程序在 AppDelegate 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14354115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com