我試圖預先在基於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);
}
對於iOS這些選項不存在。問題中的代碼沒有這些選項。 – vaichidrewar
@vaichidrewar奇怪,其實不適合我。我必須添加這些選項NSIgnorePersistentStoreVersioningOption以便能夠使用遷移的persistentStore(沒有選項,app總是拒絕打開商店)。選項詳細的iOS文檔在這裏:http://developer.apple.com/library/ios/#Documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html – macbeb
我想我的意思是說這些不是必需的iphone ios。 Xcode在開發iPhone應用程序時沒有識別這些選項。但這些應該是有效的Mac應用程序 – vaichidrewar