2013-10-31 26 views
0

將允許將外部存儲裝入UIManagedDocument的核心數據模型的正確方法是什麼?我有一個核心數據存儲,我正試圖進入UIManagedDocument。我有大量數據的用戶。其中一些是2 - 3分鐘的音頻剪輯。我正在繼承UIManaged文檔並覆蓋configurePersistentStoreCoordinatorForURL。然後將這些文件複製到UIManagedDocument束中。對於存儲在外部的音頻文件,這一切似乎都很好。在我的核心數據模型中,我的音頻文件設置爲允許外部存儲。這些文件在移動後不再連接,當我在移動後嘗試在應用程序中播放它們時,我會聽到音頻會話錯誤。感謝您提供有關該主題的任何幫助。這裏是我的代碼,我使用覆蓋UIMD ...將允許將外部存儲裝入UIManagedDocument的核心數據模型

- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)storeURL 
             ofType:(NSString *)fileType 
          modelConfiguration:(NSString *)configuration 
           storeOptions:(NSDictionary *)storeOptions 
             error:(NSError *__autoreleasing *)error{ 
    [self printFileDir]; 
    // If legacy store exists, create a UIManaged Document and store it there 
    NSURL *docsDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
    NSURL *legacyStoreURL = [docsDir URLByAppendingPathComponent:@"RRLevelBook.sqlite"]; 

    NSFileManager* fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:legacyStoreURL.path]) 
    { 
     NSLog(@"Old db exists"); 

     //swap files 
     NSURL *storeURLshm = [NSURL URLWithString:[[storeURL absoluteString] stringByAppendingString:@"-shm"]]; 
     NSURL *storeURLwal = [NSURL URLWithString:[[storeURL absoluteString] stringByAppendingString:@"-wal"]]; 
     NSURL *supportFiles = [[storeURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:@".persistenStore_SUPPORT"]; 
     NSURL *legacyStoreURLshm = [NSURL URLWithString:[[legacyStoreURL absoluteString] stringByAppendingString:@"-shm"]]; 
     NSURL *legacyStoreURLwal = [NSURL URLWithString:[[legacyStoreURL absoluteString] stringByAppendingString:@"-wal"]]; 
     NSURL *legacySupportFiles = [[legacyStoreURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:@".RRLevelBook_SUPPORT"]; 

     NSError* thisError = nil; 

     //swap the sqlite file 
     [fileManager replaceItemAtURL:storeURL 
        withItemAtURL:legacyStoreURL 
        backupItemName:nil 
          options:NSFileManagerItemReplacementUsingNewMetadataOnly 
       resultingItemURL:nil 
          error:&thisError]; 
     //swap the -shm file 
     [fileManager replaceItemAtURL:storeURLshm 
        withItemAtURL:legacyStoreURLshm 
        backupItemName:nil 
          options:NSFileManagerItemReplacementUsingNewMetadataOnly 
       resultingItemURL:nil 
          error:&thisError]; 

     //swap the -wal file 
     [fileManager replaceItemAtURL:storeURLwal 
        withItemAtURL:legacyStoreURLwal 
        backupItemName:nil 
          options:NSFileManagerItemReplacementUsingNewMetadataOnly 
       resultingItemURL:nil 
          error:&thisError]; 
     //Move in the Support files 
     [fileManager moveItemAtURL:legacySupportFiles toURL:supportFiles error:nil]; 

     //delete old files that have been swapped 
     [fileManager removeItemAtURL:legacyStoreURL error:nil]; 
     [fileManager removeItemAtURL:legacyStoreURLwal error:nil]; 
     [fileManager removeItemAtURL:legacyStoreURLshm error:nil]; 
     [fileManager removeItemAtURL:legacySupportFiles error:nil]; 

     NSLog(@"%@",[thisError localizedDescription]); 
    } 

    [self printFileDir]; 
    return [super configurePersistentStoreCoordinatorForURL:storeURL ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error]; 
} 
+0

您是否嘗試過使用的應用程序的新版本,以確認它保存音頻文件使用的是同一位置創建一個新的音頻文件? –

回答

0

好了,這裏是我終於實現了 - 是好還是壞:

  1. 打開新UIManagedDocument。
  2. 打開傳統的核心數據模型。
  3. 將來自Legacy CoreData Context的每個音頻文件(NSData)複製到UIManagedDocument上下文中。
  4. 根據傳統CoreData上下文重新連接所有關係。

    NSManagedObjectContext *legacyMOC = [[NSManagedObjectContext alloc]init]; 
    [legacyMOC setPersistentStoreCoordinator:psc]; 
    
    
    //fetch all audio recordings from legacyStore 
    NSArray *legacyRecordingArray = [self fetchAudioRecordingsfrom:legacyMOC]; 
    
    //fetch all audio recordings form UIMDStore 
    NSArray *uimdRecordingArray = [self fetchAudioRecordingsfrom:self.managedObjectContext]; 
    
    //for each audio recording, copy the audio object from legacy and save it to UIMDStore 
    for (int i = 0; i < legacyRecordingArray.count; i++) { 
    
        //save audio to core data 
        RunningRecord *legacyRR = (RunningRecord *)legacyRecordingArray[i]; 
        RunningRecord *uimdRR = (RunningRecord *)uimdRecordingArray[i]; 
        uimdRR.audioData = [NSData dataWithData:legacyRR.audio.file]; 
        uimdRR.audio.file = nil; 
    } 
    
    
    if (![self.managedObjectContext save:&error]) { 
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); 
    } 
    
相關問題