当应用程序转到iOS

时间:2017-02-02 11:03:08

标签: ios objective-c

在我的AppDelegate中,我有一个属性。它是强大的类型自定义类(它是NSObject的子类)。当应用程序转到后台时它变为NULL。此行为是不确定的,无法轻松复制(即使使用模拟内存警告选项)。它是随机发生的。

我的理解是,当应用程序进入后台并且如果其他应用程序需要内存时,应用程序将被终止。但在这种情况下,应用程序不会终止,但特定属性将变为空。

我没有在任何ViewControllers中实现didReceiveMemoryWarning。

请帮助我。

修改

这是我的AppDelegate看起来的样本

AppDelegate.h

@interface AppDelegate : UIResponder
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, strong) Person *person;//This is the custom class
@end

AppDelegate.m

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   // Override point for customization after application launch.
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   self.navigationController = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
   self.window.rootViewController = self.navigationController;
   [self.window makeKeyAndVisible];
   return YES;
}
@end

4 个答案:

答案 0 :(得分:1)

我建议你覆盖你的属性的setter并在其中添加一个断点。您会看到它何时设置为nil

Setter示例:

- (void)setPerson:(Person *)person {
    _person = person; // breakpoint on this line
}

答案 1 :(得分:0)

你没有给我们更多的信息,我不知道你在哪里设置你的Person对象。但是你应该确定它不会在某个地方被删除。对于exmp:当您为该对象分配引用时,引用可能会消失。

或者,如果您想永久保留该实例,可以使用Keychain或NSUserDefaults。

答案 2 :(得分:0)

我认为其他一些应用程序不会影响您的应用程序分配&释放。我认为你的应用程序生命周期中存在一些内存泄漏,或者可能是某些其他对象指向你的Person类弱,然后当应用程序进入后台状态时没有人指出你的对象时释放你的对象。

答案 3 :(得分:0)

如果应用程序终止,那么人们会松开person对象,因此在使用之前保持对象并获取。为了保持持久性,您可以使用任何数据库或文件系统,如plist或用户默认值。