2013-01-18 15 views
5

コアデータを処理するために、UIManagedDocumentに基づいたアプリケーションで永続ストアを事前にロードしようとしています。私は、アプリBに使用しようiOS UIManagedDocument:プリロードされた永続ストアを開くことができません

永続ストアは、「生成」されると、アプリAとBの両方でアプリのA. に人口の多いおかげで私はジャスティン・ドリスコルからUIManagedDocumentハンドラを使用(available here、ありがとうミスタードリスコル!)。すべては完全にアプリで動作します。

Pre-load core data database in iOS 5 with UIManagedDocumentのテクニックに基づいて、私は永続ストアをBのアプリケーションバンドルに入れ、必要に応じてこのストアをドキュメントフォルダにコピーしようとします以前にインスタンス化されています)。

バンドルからドキュメントへのコピーはすべてOKです(私は別の方法を試して、finderとnslogの作成を確認しました)。しかし、私は "ドキュメント"を開くことができません。 アプリケーションはクラッシュしません。ビューは表示されますが、テーブルは空です(私はアプリケーションAと同じコードを同じfetchedResultsControllerで使用します)。最初に私はコピーされた永続ストアが空であると思ったが、文書/コピーされた永続ストアを正しく開くことができないことに気付いた) =>ドキュメントの状態= 5、UIDocumentStateClosedとUIDocumentStateSavingErrorのエラーを意味する。 ?)

(注:私はまた、バンドルから直接ドキュメントをインスタンス化して開くように試みたと私は同じ問題持っている:DOC状態= 5)

だから... ...三日と戦っこのドキュメントの状態= 5と何を修正するかについての手がかりなし

私はアプリBのバンドルに入れたファイルに何か問題があると想像します(現在ドラッグ&ドロップします「追加されたフォルダのフォルダ参照を作成する」を選択してxcodeにファインダを選択) 多分、いくつかのオプション、メタデータ、またはファイルのアクセス許可についてです...

何を調査すべきですか?

私はinit(Justin Driscollハンドラをベースにしていますが、custoのみです:ドキュメントフォルダにストアパッケージがあるかどうかを確認します。私は氏ドリスコルが提供するperformWithDocumentコードに作られた

- (id)init 
{ 
self = [super init]; 
if (self) { 
    self.document = nil; 

    NSLog(@"--- INIT ---"); 

    // On vérifie si il y a un dossier "My-Default-Document-As-Database" (notre persitent store) dans le dossier "Documents" 

    NSString *docDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *docFolderPath = [docDirectory stringByAppendingPathComponent:@"My-Default-Document-As-Database"]; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:docFolderPath]) { 
     NSError *error = nil; 
     NSLog(@"Pas de fichier trouvé à l'adresse docFolderPath, on va donc y créer notre persistent store"); 

     // COPY FROM BUNDLE 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     NSString *DB = [docFolderPath stringByAppendingPathComponent:@"StoreContent"]; 

     [fileManager createDirectoryAtPath:DB withIntermediateDirectories:YES attributes:nil error:&error]; 

     NSLog(@"create directory error: %@",error); 

     DB = [DB stringByAppendingPathComponent:@"persistentStore"]; 

     NSString *shippedDB = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"persistentStore"]; 

     NSLog(@"%d",[fileManager fileExistsAtPath:shippedDB]); 

     [fileManager copyItemAtPath:shippedDB toPath:DB error:&error]; 

     NSLog(@"Copy error %@",error); 

    } 

    NSLog(@"== My-Default-Document-As-Database OK DANS DOCUMENTS =="); 

    NSURL *myDbUrl = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 

    myDbUrl = [myDbUrl URLByAppendingPathComponent:@"My-Default-Document-As-Database/"]; 

    self.document = [[UIManagedDocument alloc] initWithFileURL:myDbUrl]; 

    NSLog(@"== initWithFileUrl =="); 

      // Set our document up for automatic migrations 
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
            [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 
      self.document.persistentStoreOptions = options; 


      // Register for notifications 
      [[NSNotificationCenter defaultCenter] addObserver:self 
                selector:@selector(objectsDidChange:) 
                 name:NSManagedObjectContextObjectsDidChangeNotification 
                 object:self.document.managedObjectContext]; 

      [[NSNotificationCenter defaultCenter] addObserver:self 
                selector:@selector(contextDidSave:) 
                 name:NSManagedObjectContextDidSaveNotification 
                 object:self.document.managedObjectContext]; 
} 
return self; 
} 

のみ「変更」)バンドル内のファイルに基づいて作成していないhapening何を参照するには、いくつかのNSLog(ドキュメントの状態は、すべての上に1から5に行くされています最初に試してみてから5に固執してください...)

答えて

2

compadreのおかげで答えは...誰かがそれを探すならば:

これはオプションでした!

NSIgnorePersistentStoreVersioningOptionをinitのpesistenStoreオプションに追加します。

前のコードについて、あなたはこのような何かを持っている必要があります。これらのオプションは存在しないiOSの

// Set our document up for automatic migrations 
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
    [NSNumber numberWithBool:YES], NSIgnorePersistentStoreVersioningOption, 
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 
self.document.persistentStoreOptions = options; 
+0

を。質問のコードはこれらのオプションなしで機能しました。 – vaichidrewar

+0

@vaichidrewar奇妙な、実際には私のためではありません。これらのオプションNSIgnorePersistentStoreVersioningOptionを追加して、移行されたpersistentStoreを使用できるようにする必要があります(オプションなしで、アプリケーションは常にストアを開くことを拒否します)。オプションについては、こちらのiOS docで詳しく説明しています。http://developer.apple.com/library/ios/#Documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html – macbeb

+0

私はこれらが必要ではないと言っていたと思いますiphone ios。 XcodeはiPhoneアプリの開発中にこれらのオプションを認識しませんでした。しかし、それらはMacアプリケーションのために有効であるべきです – vaichidrewar

関連する問題