- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新 1:当 iOS 的数据保护(即密码锁定)关闭时不会出现问题!
更新 2:禁用 ARC 时不会出现问题。
更新 3:在重新启动 iOS 之前杀死应用程序时不会发生问题。
我的 Settings 类使用 +initialize 实现了单例模式(加上代码以查看发生了什么):
@implementation Settings
static Settings* sharedSettings;
static NSUserDefaults* userDefaults;
+ (void)initialize
{
if ([Settings class] == self)
{
sharedSettings = [self new];
userDefaults = [NSUserDefaults standardUserDefaults];
// Code to see what's going on here ...
if (userDefaults == nil)
{
[[[UIAlertView alloc] initWithTitle:@"userDefaults == nil"
message:nil
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil] show];
}
else
{
if ([userDefaults objectForKey:@"Hello"] == nil)
{
[[[UIAlertView alloc] initWithTitle:@"Hello == nil"
message:nil
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil] show];
}
else if ([userDefaults boolForKey:@"Hello"] == NO)
{
[[[UIAlertView alloc] initWithTitle:@"Hello == NO"
message:nil
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil] show];
}
[userDefaults setBool:YES forKey:@"Hello"];
if ([userDefaults synchronize] == NO)
{
[[[UIAlertView alloc] initWithTitle:@"synchronize == NO"
message:nil
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil] show];
}
}
}
}
+ (id)allocWithZone:(NSZone*)zone
{
if (sharedSettings && [Settings class] == self)
{
[NSException raise:NSGenericException format:@"Duplicate Settings singleton creation"];
}
return [super allocWithZone:zone];
}
+ (Settings*)sharedSettings
{
return sharedSettings;
}
@end
#import "AppDelegate.h"
#import "Settings.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[Settings sharedSettings];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
return YES;
}
@end
最佳答案
在 +initialize
期间不应引用其他类.没有关于它何时运行以及当时存在哪些其他类的 promise 。 +initialize
应该只做内部对类(class)的工作。
自添加 GCD 以来,这种单例模式已被取代。您应该出于各种原因使用 GCD 模式,其中之一是它在您认为它运行时运行。
How do I implement an Objective-C singleton that is compatible with ARC?
作为说明,我很好奇你的main.mm
.制作顶级 ObjC++ 几乎总是一个坏主意,并且可能导致一些令人惊讶的副作用。 (ObjC++ 是一种古怪的胶水语言,具有许多奇怪的行为。您应该在尽可能少的类中使用它。)我个人会看看是否将其改回 .m
解决了这个问题,虽然我仍然推荐 GCD-Singleton 而不是使用 +initialize
.
关于iphone - 数据保护开启 : standardUserdefaults corrupted after iOS restart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15450072/
我的 Windows 使用 Ubuntu 设置了 WSL。它已经工作了一段时间,直到我升级到 WSL2。 问题是:每次我启动我的电脑(或重新启动我的电脑)时,它都会失去从 Ubuntu 到 Windo
我正在使用 docker 版本 1.1.0,由 systemd 使用命令行启动 /usr/bin/docker -d ,并试图: 运行容器 停止 docker 服务 重新启动 docker 服务(使用
鉴于以下(为了论证而简化)docker-compose.yml文件: version: '3' services: postgres: image: fleetit-postgres
我使用 Vertx SockJs 构建了一个 Eventbus 桥。 这是我的verticle的代码: @Override public void start() throws Exception {
使用docker stack deploy,我可以看到如下信息: Ignoring unsupported options: restart 是否意味着重启政策没有到位? 是否必须在撰写文件之外指定它
我们的 Varnish 实例 /usr/sbin/varnishd -P /var/run/varnish.pid -a :6081 -f /etc/varnish/cm-varnish.vcl -
Varnish 是否保留崩溃/重启日志? 我目前正在监控一个 varnish 服务器,它似乎每周都会重新启动,当 CPU 使用率达到大约 100%(负载有点高 - 在 2 核机器上大约 6~7)并且
我有一个 Android 应用程序,在您第一次运行时运行良好。但是,当您按 HOME(因此暂停)并从主屏幕重新启动它时,我得到的只是音乐重新启动。 我还没有进入保存/恢复状态的阶段(很快就会完成!)。
我的应用程序有一个服务,每 X 分钟对数据库执行与 stopSelf() 相同的操作,并进入 onDestroy 方法我已经放置了这段代码以便在同一时间后重新启动服务: @Override publi
复制代码 代码如下: '******************************************************************** '* '* 
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我检查了我在 ECS 集群中运行的一个 docker 容器,它的重启策略为空白(“{{ .HostConfig.RestartPolicy.Name }}”)。我不确定 ECS 在发生故障时会做什么,
我用 Google Cloud Shell运行 Jupyter Notebook在实例上,但在 1 hours 之后,连接断开。 我重新连接到实例,什么都没有,与重置实例相同。 So, please
Java中是否有任何命令或程序可以重新启动我的应用程序本身? 我开发了一个 Java 桌面应用程序,但过了一段时间它就会挂起。我不知道为什么,所以我想重新启动我的应用程序本身。是否可以? 最佳答案 你
我已经创建了一个容器,却忘记了在创建容器时设置--restart ='always'。 是否可以为已经创建的容器修改此选项?我知道我可以创建一个新的,但这很有用,因为它发生在我身上好几次了... 最佳
是否可以使用 pm2 重新启动特定组的应用程序?像 pm2 restart service-* 重新启动 service-a 和 service-b 之类的东西?我发现 pm2 restart ser
我是一个刚开始使用C++编写代码已经有几个星期了。我已经编写了一个程序来拥有一个菜单,用户可以从中选择执行不同任务的选项。我有两个问题:首先,我该如何做,以便在执行任务后将用户发送回菜单;其次,当用户
所以我有一个应用程序,用户通过 3 个 View Controller 然后向 Facebook 提交图片。在他们将其提交到 Facebook 后,我希望他们能够选择重新启动该过程,就好像他们重新启动
我正在尝试构建一个 hadoop Dockerfile。 在构建过程中,我添加了: && apt install -y openssh-client \ && apt install -y o
当您运行docker run时,可以选择添加--rm和--restart。但是,如何在dockerfile中获得相同的效果? 最佳答案 Dockerfile用于指定构建镜像的指令。 生成镜像后,可以使
我是一名优秀的程序员,十分优秀!