Domanda

I've done quite a bit of research in to other people with similar problems, solutions provided for them have failed to resolve my issue so I'm thinking its something obvious that's missing, or I am verifying something incorrectly and inappropriately trying to identify my error.

My problem is that a .plist file that I have added in order to have persistent data storage for my application is not being found when I try to programatically save/load from it.

I have:

  • verified that it is listed under "Copy Bundle Resources" in the Build Phases tab of my project.
  • verified that the file is being copied to my /Users/Me/Library/Application Support/iPhone Simulator/5.1/Applications/Unique App ID/myapp.app folder, and if deleted and rerun it appears once again.
  • cleaned and rebuilt my project

My code is organized such that saving and loading take place in two different spots. Load happens when a modal view is brought up and the user is able to select various sets of data. Saving happens in the application delegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsfunction. I have verified that both of these functions are called, although the behavior has me totally stumped.

Fairly boilerplate save and load code heavily based of off the developer resources plist article:

- (void)applicationDidEnterBackground:(UIApplication *)application
{    
    NSDictionary *someDataToSave = [NSDictionary dictionaryWithObject:self.viewController.dataStorage forKey:@"myData"];

    NSString *error;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"someData.plist"];
    plistPath = [rootPath stringByAppendingPathComponent:@"someData.plist"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"someData.plist" ofType:@"plist"];
    }

    NSString *generatedName = [NSString stringWithFormat:@"DataSet - %s", [NSDate date]];

    NSDictionary *plistDict = [NSDictionary dictionaryWithObject:someDataToSave forKey:generatedName];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    if(plistData)
        [plistData writeToFile:plistPath atomically:YES];
    else {
        NSLog(error);
    }
}

-(void)loadRecordsFromPList
{

    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    plistPath = [rootPath stringByAppendingPathComponent:@"someData.plist"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"someData.plist" ofType:@"plist"];
    }

    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];

    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }

    self.myRecordsList = [NSMutableDictionary dictionaryWithDictionary:temp];    
}

I have spent hours in the debugger trying various things and nothing I have tweaked got me anywhere. fileExistsAtPath consistently returns null for no discernible reason, and the file is not found despite me having seen it in the folder. Additionally, I have created a single view application containing nothing more than a plist file, and a view controller in which I use 99% of the same code as above, and it works without issue.

Any help that anyone can give would be very much appreciated. At this point I've tried everything I can think of and then some.

È stato utile?

Soluzione

In applicationDidEnterBackground, you set plistPath to the path inside the main bundle if there is no plist file in the Documents directory. And then you use the same path for writing the plist file. In other words, you try to write the dictionary to the plist file in the applications main bundle.

Only in loadRecordsFromPList it makes sense to look the Documents folder first and then in the main bundle. When you save the data, you must always save to the Documents folder.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top