1

アプリで一連の配列からデータを保存およびロードするようにアプリを取得しようとしましたが、奇妙な問題は、アプリが終了 (完全にシャットダウン) すると、再起動時にデータがロードされないように見えることです。多くの投稿、チュートリアルなどを見てきましたが、動作させることができないようです。保存メソッドと読み込みメソッドをトリガーするための 2 つのテスト ボタンがアプリにあり、レコードをクリアするボタンもあります。これらを使用してテストすると、完全に機能し、データが正しく保存およびロードされます。

私の現在の設定は次のとおりです。

  • サポート ファイル ディレクトリに 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");
}

これは文字通り私に髪を引っ張らせているので、どんな助けも素晴らしいでしょう!

4

1 に答える 1

1

このアプリのデリゲート メソッドで、起動時にデータを読み込むことができます。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   ....
   [[GlobalData sharedGlobalData] loadData];
}
于 2012-12-26T14:19:55.153 に答える