我一直试图让我的应用程序从应用程序中的一组数组中保存和加载数据,奇怪的问题是,当应用程序终止(完全关闭)时,数据似乎在重新启动时不会加载,我已经看了很多帖子,教程等,但我似乎无法让它工作,我有两个测试按钮在应用程序上触发保存和加载方法,还有一个按钮来清除记录,当我使用它们进行测试时,它可以正常工作,数据可以正确保存和加载。
我目前的设置如下:
我的保存代码:
- (void)saveData{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"data.plist"];
// create dictionary with arrays and their corresponding keys
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personalitySliderValue, looksSliderValue, humourSliderValue, chemistrySliderValue, emptySlider, notesValues,nameValues, noValues, ratingValues, nil] forKeys:[NSArray arrayWithObjects: @"personality", @"looks", @"humour", @"chemistry",@"empty",@"notes",@"name",@"no",@"rating", nil]];
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check if plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error in saveData: %@", error);
}
}
我的加载代码:
- (void)loadData{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"data.plist"];
// check to see if data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle CHECK D capitalisation
plistPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
NSDictionary *dictionaryTemp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!dictionaryTemp)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
// assign values
personalitySliderValue = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"personality"]];
looksSliderValue = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"looks"]];
humourSliderValue = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"humour"]];
chemistrySliderValue = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"chemistry"]];
emptySlider = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"empty"]];
notesValues = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"notes"]];
nameValues = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"name"]];
noValues = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"no"]];
ratingValues = [NSMutableArray arrayWithArray:[dictionaryTemp objectForKey:@"rating"]];
}
最后是app委托方法:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// save the app data
[[GlobalData sharedGlobalData]saveData];
NSLog(@"save method run");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
// load the app data
[[GlobalData sharedGlobalData]loadData];
NSLog(@"load method run");
}
这实际上让我脱掉了头发,所以任何帮助都会很棒!
答案 0 :(得分:1)
您可以在此应用委托方法中以发布时加载数据:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
....
[[GlobalData sharedGlobalData] loadData];
}