Reputation: 143
EDIT: Doesn't look like it's working with the simulator either now.
Some more info, It seems if I install an archived version that did work, then install the one that wont work, right over it, everything works great. but when I delete the archived version, and install the new one, thats when everything stops working.
I was just testing my app on my iPhone, and it was working perfectly, and when I archived it, and installed the ipa on my phone, the database stopped working. no errors occur when you load the database, and like I said before, it was JUST working. I didn't change ANY code. it still works on the simulator, so I know it has to do with the copying of the database. here's the relevant code:
dbPath=[NSString stringWithFormat:@"%@/Documents/database.sql", NSHomeDirectory()];
// Get the documents directory
NSFileManager *fmngr = [[NSFileManager alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"database.sql" ofType:nil];
NSError *error;
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"didLaunchFirstTime"])
{
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"didLaunchFirstTime"];
[fmngr removeItemAtPath:[NSString stringWithFormat:@"%@/Documents/database.sql", NSHomeDirectory()] error:&error];
if(![fmngr copyItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/Documents/database.sql", NSHomeDirectory()] error:&error])
{
// handle the error
NSLog(@"Error creating the database: %@", [error description]);
}
}
My query looks like this, because I'm using FMDB to query the database. It's in a separate method called when the user presses the search button.
FMResultSet *s = [db executeQueryWithFormat:@"SELECT Gurmukhi, ShabadID, FirstLetterStr FROM Shabad WHERE FirstLetterStr LIKE %@", searchString];
I also unzipped the ipa to check if the database wasn't blank, and it isn't. I have no idea what's going on.
Upvotes: 0
Views: 120
Reputation: 41642
You should not be hardcoding directory paths in your app - Apple provides functions to get them:
When you want to find the file in your bundle, you get it this way:
NSString *dbPathOld = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"sql"];
Now you have a path to the sql file you provided with your app.
When you want to copy it, you use this code:
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *dbPathNew = [docDir stringByAppendingPathComponent:@"database.sql"];
Other comments:
1) Why not use the NSFileManager *fm = [NSFileManager defaultManager];?
2) With your standard defaults, you are registering them in an initialize method in your app delegate? You are synchronizing them after you update values (so in fact "didLaunchFirstTime" is set the second time)? You might want to add a log message so you can know for sure.
Upvotes: 1