2011-08-01 8 views
1

私はsql lite dbで使用しているコアデータアプリケーションを持っています。私はそれを私のシミュレータに移植しましたが、今は私がipadにインストールするたびに、しかしのアプリを送ってみたいと思います。populated sql lite dbがipadに転送されると空白になる

は、私は(シミュレータフォルダから)プロジェクトにデシベル人口を追加しましたし、私は、デリゲートに次き:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
[window addSubview:splitViewController.view]; 
[window makeKeyAndVisible]; 

NSString *Path = [[NSBundle mainBundle] bundlePath]; 
NSString *DataPath = [Path stringByAppendingPathComponent:@"data.plist"]; 

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath]; 
self.data = tempDict; 
[tempDict release]; 

// Override point for customization after application launch. 
// Add the split view controller's view to the window and display. 
[self createEditableCopyOfDatabaseIfNeeded]; 


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"NO" forKey:@"enableRotation"]; 
[appDefaults setObject:@"0" forKey:@"orientation"]; 
[appDefaults setObject:@"100" forKey:@"repID"]; 
[appDefaults setObject:@"Atlanta Dental" forKey:@"RepName"]; 
[defaults registerDefaults:appDefaults]; 

//self.window.rootViewController = self.splitViewController; 
return YES; 
} 

-(void)createEditableCopyOfDatabaseIfNeeded 
{ 
// Testing for existence 
BOOL success; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME]; 

success = [fileManager fileExistsAtPath:writableDBPath]; 
if (success) 
    return; 

// The writable database does not exist, so copy the default to 
// the appropriate location. 
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] 
          stringByAppendingPathComponent:DATABASE_NAME]; 
success = [fileManager copyItemAtPath:defaultDBPath 
           toPath:writableDBPath 
           error:&error]; 
if(!success) 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // If the expected store doesn't exist, copy the default store. 
    //if (! [fileManager fileExistsAtPath:storePath]) 
    //{ 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"D4" ofType:@"sqlite"]; 
     if (defaultStorePath) 
     { 
      [fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath error:NULL]; 
     } 
    //} 
    //NSAssert1(0,@"Failed to create writable database file with Message : '%@'.", 
    //  [error localizedDescription]); 
} 
} 


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
if (persistentStoreCoordinator != nil) 
{ 
    return persistentStoreCoordinator; 
} 

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:DATABASE_NAME]; 

NSError *error = nil; 
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
{ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
}  

return persistentStoreCoordinator; 
} 

任意のアイデア?

追加するEDITED:

を私はiphoneエクスプローラでのiPadを探索するとき、私はアプリのパスに、元の人口のDBを参照することができ、それがコピーされてきています。それは私が上記の間違っていなければならないことを意味します。 。

右か?

答えて

1

、で参照してください。不足しているストアの最も可能性の高い説明は、コピーされていると思われる場所にコピーされていないということです。

アプリケーションバンドル内のデフォルトファイルまたはドキュメント内のreadwriteファイルへのパスを取得するために、同じメソッドを2回使用することはありません。パスを作成するたびに、別のコードを使用します。これにより、コードの異なるポイントで異なるパスを生成することが可能になります。各パスを生成する方法を1つ選択し、必要に応じて再利用します。

すべてのパスとURLをロギングして、自分の思うところにすべての点があることを確認することをお勧めします。また、ファイル管理操作のエラーをトラップすることをお勧めします。ブールリターンがYESの場合でもエラーが発生する可能性があります。

+0

ポピュレートされたものをコピーしようとする前に、ストアにアクセスしていました。良いコール。 –

0

ここに解決策があります。ポピュレートされたdbをシミュレータからコピーしてプロジェクトにコピーします。その後、デバイスからアプリを削除し、新しいデータベースをコピーします。ブランクdbが既に存在する場合は、アプリはデータをコピーします。それは、新しい空のストアを作成し、あなたが提供するURLで既存店を見つけることができません永続ストアコーディネータを意味し、コードストアは常に空白ですので

NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME]; 

success = [fileManager fileExistsAtPath:writableDBPath]; 
if (success)//database already copied 
    return; 
関連する問題