0

I've been working on this problem for a week, and I googled and searched stack overflow, read about 40 posts, still can't fix my problem. here is what i did: 1.I wrote a testing app to create the sqlite database, and preload it with data. 2.I create myApp, and copied the preloaded DB to resource folder. 3.I wrote the following code to get the DB:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (!self.myDataBase) {
        NSURL *url = [self localDocumentsDirectoryURL];
        url = [url URLByAppendingPathComponent:@"myDB/"];
        self.myDataBase = [[UIManagedDocument alloc] initWithFileURL:url];
    }

    return YES;
}

-(NSURL*)localDocumentsDirectoryURL {
    static NSURL *localDocumentsDirectoryURL = nil;
    if (localDocumentsDirectoryURL == nil) {
        NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES ) objectAtIndex:0];
        localDocumentsDirectoryURL = [NSURL fileURLWithPath:documentsDirectoryPath];
    }
    return localDocumentsDirectoryURL;
}

- (void)useDocument
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.ICCarDataBase.fileURL path]]) 
    {
        [self.ICCarDataBase saveToURL:self.ICCarDataBase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {}];
    } 
    else
    {

        [self.ICCarDataBase openWithCompletionHandler:^(BOOL success) {}];
    }
}

- (void) setICCarDataBase:(UIManagedDocument *)carDataBase
{
    if (_ICCarDataBase != carDataBase) {
        _ICCarDataBase = carDataBase;
        [self useDocument];
    }
}

Then when I run myApp in Simulator, myApp successfully get the DB data, and when I run it in my iPhone, the myApp can't get the DB data. I don't know if the SDK version matters, coz, the simulator is iphone 5.1, and my iPhone is 5.0 (jailbreak). And I don't use any 5.1 specific function in my App.

Some says that you should copy the DataBase to document dir first for it to work, I've tried the solution, still works ok in simulator, but no data in iPhone. Plus, I looked into iphone folders using iTool, myDB folder is already in the document dir.The post suggest the solution is here:Pre-load core data database in iOS 5 with UIManagedDocument

Other says I should use persistentStoreCoordinator directly. But I think UIManagedDocument could work in my situation, since it create a implicit persistentStoreCoordinator itself.

Any suggestion about what's wrong with myApp?

And anyone can tell me why ios wrap the sqlite DB with two layers of folders, and name the actual sqlite db persisentStore?

Community
  • 1
  • 1
frankskywalker
  • 85
  • 1
  • 11

1 Answers1

0

I've solved this problem, thanks to this postiPhone: Can access files in documents directory in Simulator, but not device

In this post he referenced this blog :Xcode resource groups and folder references when building for iPhone

After solving my problem, I've come to realize the reason that preload sqlite database won't work in device is: when Xcode copy database to the device from the bundle, the folder layers(myDB/StoreContent/persistentStore) wrapping the persistentStore is removed. So there is only a persistentStore file mixed with all other files in the device in the myApp.app bundle. And when you use UIManagedDocument to access sqlite database, it can only work with sqlite in such directory structure: myDB/StoreContent/persistentStore. So, with only a naked persistentStore file, the UIManagedDocument will create a whole new directory structure for you, with an empty persistentStore in it.

If someone can suggest a way for UIManagedDocument to work with the persistentStore file without folder layers, I would be interested.

And I use this code to access the DB after I successfully replicate the directory structure in my device bundle:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (!self.myDataBase) {
        NSURL *url = [[NSBundle mainBundle] resourceURL];
        url = [url URLByAppendingPathComponent:@"myDB/"];
        self.myDataBase = [[UIManagedDocument alloc] initWithFileURL:url];
    }

    return YES;
}
Community
  • 1
  • 1
frankskywalker
  • 85
  • 1
  • 11