gpt4 book ai didi

ios - 重新启动应用程序后 iOS 上的随机数据丢失

转载 作者:行者123 更新时间:2023-11-28 17:42:22 25 4
gpt4 key购买 nike

显然我一定没有做对,但这种情况偶尔会发生。我无法按照某些步骤使问题出现,因此调试变得非常困难。我有一个应用程序,每次添加或删除对象时,它都会将文件写入 plist。我可以在模拟器中验证 plist,并且每次调用我的保存函数时我也有一个 NSLog。调用 applicationWillEnterForeground 时加载数据,这也正常工作。在启动应用程序后的某些情况下,它会恢复到最近更改之前的先前保存。 iOS 是否缓存数据文件?如果它尝试将文件从磁盘加载到阵列,是否可能已经在缓存中加载了先前的加载并使用该数据创建阵列?

保存方法:

- (void)saveFile {
// saves data to file

NSLog(@"save file reached");
#ifdef LITE_VERSION

[self.aquariums writeToFile:[self dataFilePathLite] atomically:YES];

#else

[self.aquariums writeToFile:[self dataFilePath] atomically:YES];

#endif
}

加载方法;我刚刚添加了 if (self.aquariums == null) 检查,它可能已经解决了问题,但我很难确认,因为我无法重现问题:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/

NSLog(@"applicationDidBecomeActive called");

if (self.aquariums == NULL) {
NSLog(@"aquariums null, loading file");

#ifdef LITE_VERSION
NSString *filePath = [self dataFilePathLite];
#else
NSString *filePath = [self dataFilePath];
#endif

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
self.aquariums = array;
[array release];

[self update21];

} else {

#ifdef LITE_VERSION
[self convertFileName];

#else
[self update20];
#endif
}

application.applicationIconBadgeNumber = 0;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
aquaPlannerAppDelegate_iPad *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.rootView viewDidAppear:YES];
}
}
}

最佳答案

发布后没有收到任何数据丢失报告,问题肯定是

if (self.aquariums == NULL) {

所以人们记得如果您要在应用程序从后台返回时重新加载数据,请先将其设置为 null 然后发布

aquariums = NULL;
[aquariums release];

关于ios - 重新启动应用程序后 iOS 上的随机数据丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7623695/

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