2012-12-26 71 views
1

我一直在試圖讓我的應用程序保存和從應用程序中的一組數據加載數據,奇怪的問題是,當應用程序終止(完全關閉)數據似乎並不加載後重新啓動,我看了很多帖子,教程等,但我似乎無法得到它的工作,我有兩個測試按鈕在應用程序觸發保存和加載方法,也是一個按鈕清除記錄,當我使用這些來測試時,它可以正常工作,並且數據可以正確保存和加載。保存/加載IOS數據

我目前的設置如下:

  • 我叫data.plist在支持文件目錄中的plist文件,這個文件中我有相同的數據在不同的陣列中的索引0的數據當全局數據類創建它自己的一個實例時它被初始化。

我節省代碼:

- (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"]]; 

} 

最後應用程序的委託方法:

- (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

作爲user1885297說,'didFinishLaunchingWithOptions'加載它。這樣你就可以確信它在用戶界面出現之前就被加載了。順便說一句,如果完全刪除對「NSData」的引用,並且使用'NSDictionary'實例方法'writeToFile'並使用類方法'dictionaryWithContentsOfFile'加載,則可以簡化代碼。 – Rob

+0

感謝羅布我可能會做出上述改變,我很難得到這個工作正常,我最終使用的教程可能有點過時。 – SmokersCough

回答

1

您可以在此應用程序的委託方法加載在啓動時的數據:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    .... 
    [[GlobalData sharedGlobalData] loadData]; 
}