2013-01-18 28 views
5

我试图预先在基于UIManagedDocument的应用程序中加载持久存储来处理核心数据。iOS UIManagedDocument:无法打开预加载的持久性存储

持久性商店,我尝试在应用B使用时,“生成”,我用从贾斯汀·德里斯科尔的UIManagedDocument处理填充由于应用A. 在这两个应用程序A和B(available here,感谢先生德里斯科尔!)。所有作品完美地在应用程序A.

基于在此线程中解释的技术:Pre-load core data database in iOS 5 with UIManagedDocument,我尝试将持久性存储放在B的应用程序包中,并在需要时将该存储复制到文档文件夹中(如果不是在init之前完成)在实例化之前完成。

从包复制到文件都可以(我尝试了不同的方法,并通过finder和nslog检查创建),但我无法打开“文档”。 应用程序不会崩溃,视图显示,但表格是空的(我使用完全相同的代码作为应用程序A,具有相同的fetchedResultsController)。首先,我认为复制的持久性存储是空的,然后我意识到我无法正确打开文档/复制的持久存储) => Document state = 5,意味着UIDocumentStateClosed和UIDocumentStateSavingError的错误(如果我正确地解释它? ?)

(注:我也尝试实例化,并直接从包打开文档和我有同样的问题:DOC状态= 5)

所以......三天战斗这个文件状态= 5,并不知道要修复什么

我想我的应用程序包B中的文件存在问题(目前我从中拖放取景器xcode与“为所有添加的文件夹创建文件夹引用”选择) 也许这是关于一些选项,或元数据,或文件权限或...

任何想法什么调查? (我不认为它是关于下面的代码,但是...) 这里是我如何初始化(基于贾斯汀Driscoll处理程序。只有客户是:我检查是否有文档文件夹中的商店包,如果不是我创造它基于束

- (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; 
} 

只有“修改”我对斯科尔先生提供的performWithDocument代码所做的文件)有一些的NSLog,看看有什么hapening(DOC状态从1到5在每先打开试试然后坚持到5 ...)

- (void)performWithDocument:(OnDocumentReady)onDocumentReady 
{ 
NSLog(@"passage par performWithDoc"); 

void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success) { 
    NSLog(@"FilePath Apres = %@",[self.document.fileURL path]); 
    NSLog(@"STATE === %d", self.document.documentState); 
    onDocumentReady(self.document); 
}; 

NSLog(@"FilePath Avant = %@",[self.document.fileURL path]); 
NSLog(@"STATE === %d", self.document.documentState); 

if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) { 
    [self.document saveToURL:self.document.fileURL 
      forSaveOperation:UIDocumentSaveForCreating 
      completionHandler:OnDocumentDidLoad]; 
    NSLog(@"performWithDoc > fileexistAtPath nope => saveToURLForCreating"); 
    NSLog(@"STATE === %d", self.document.documentState); 
} else if (self.document.documentState == UIDocumentStateClosed) { 
    [self.document openWithCompletionHandler:OnDocumentDidLoad]; 
    NSLog(@"performWithDoc > UIDocStateClosed => openWithCompletionHandler"); 
    NSLog(@"STATE === %d", self.document.documentState); 
} else if (self.document.documentState == UIDocumentStateNormal) { 
    OnDocumentDidLoad(YES); 
    NSLog(@"performWithDoc > docState = normal => docdidLoad(YES)"); 
} 
NSLog(@"STATE === %d", self.document.documentState); 
} 

回答

2

感谢compadre,这里是答案...如果有人搜索它:

这是关于选项!

NSIgnorePersistentStoreVersioningOption添加到init中的pesistenStore选项。

对于前面的代码,你应该有这样的事情:

// 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

对于iOS这些选项不存在。问题中的代码没有这些选项。 – vaichidrewar

+0

@vaichidrewar奇怪,其实不适合我。我必须添加这些选项NSIgnorePersistentStoreVersioningOption以便能够使用迁移的persistentStore(没有选项,app总是拒绝打开商店)。选项详细的iOS文档在这里:http://developer.apple.com/library/ios/#Documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html – macbeb

+0

我想我的意思是说这些不是必需的iphone ios。 Xcode在开发iPhone应用程序时没有识别这些选项。但这些应该是有效的Mac应用程序 – vaichidrewar