2013-10-31 23 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]); 
    } 
    
相关问题