2010-10-06 68 views
1

我正在使用核心數據,我的應用在模擬器中完美工作,但在實際設備上數據庫爲空。從模擬器到設備的核心數據遷移

我啓用CoreData日誌記錄,所以我看到實際的表正在創建,查詢運行但所有的查詢都是空的。我還從設備中複製了該應用程序,並驗證了架構已創建,但沒有數據。

我錯過了什麼數據實際重新填充?

的代碼已更新

- (NSDictionary*)migrationOptions { 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 
    return options; 
} 

- (NSManagedObjectContext*)managedObjectContext { 
    if(_managedObjectContext != nil) { 
     return _managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     _managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [_managedObjectContext setPersistentStoreCoordinator: coordinator]; 
     [_managedObjectContext setUndoManager:nil]; 
     [_managedObjectContext setRetainsRegisteredObjects:YES]; 
    } 
    return _managedObjectContext; 
} 


- (NSManagedObjectModel*)managedObjectModel { 
    if(_managedObjectModel != nil) { 
     return _managedObjectModel; 
    } 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"momd"]; 
    NSURL *momURL = [NSURL fileURLWithPath:path]; 

    //_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; 
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; 

    return _managedObjectModel; 
} 


- (NSPersistentStoreCoordinator*)persistentStoreCoordinator { 
    if(_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    NSString* storePath = [self storePath]; 
    NSURL *storeUrl = [self storeUrl]; 


    NSError* error; 
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 

    NSDictionary* options = [self migrationOptions]; 

    // Check whether the store already exists or not. 
    NSFileManager* fileManager = [NSFileManager defaultManager]; 
    BOOL exists = [fileManager fileExistsAtPath:storePath]; 

    TTDINFO(storePath); 
    if(!exists) { 
     _modelCreated = YES; 
    } else { 
     if(_resetModel || 
      [[NSUserDefaults standardUserDefaults] boolForKey:@"erase_all_preference"]) { 
      [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"erase_all_preference"]; 
      [fileManager removeItemAtPath:storePath error:nil]; 
      _modelCreated = YES; 
     } 
    } 

    if (![_persistentStoreCoordinator 
      addPersistentStoreWithType: kStoreType 
      configuration: nil 
      URL: storeUrl 
      options: options 
      error: &error 
      ]) { 
     // We couldn't add the persistent store, so let's wipe it out and try again. 
     [fileManager removeItemAtPath:storePath error:nil]; 
     _modelCreated = YES; 

     if (![_persistentStoreCoordinator 
       addPersistentStoreWithType: kStoreType 
       configuration: nil 
       URL: storeUrl 
       options: nil 
       error: &error 
       ]) { 
      // Something is terribly wrong here. 
     } 
    } 

    return _persistentStoreCoordinator; 
} 


- (NSString*)storePath { 
    return [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kStoreFilename]; 
} 


- (NSURL*)storeUrl { 
    return [NSURL fileURLWithPath:[self storePath]]; 
} 
+0

如何'storePath'和'storeUrl'設置?他們的價值是什麼? – 2010-10-06 22:25:59

回答

0

好的,所以經過如此多的搜索後終於找到了解決方案。

基本上它是從模擬器複製原始的sql文件到設備。

http://iphonedevelopment.blogspot.com/2010/08/core-data-starting-data.html

這裏的商店持久創造協調員:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    @synchronized (self) 
    { 
     if (persistentStoreCoordinator != nil) 
      return persistentStoreCoordinator; 

     NSString *defaultStorePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"My_App_Name" ofType:@"sqlite"]; 
     NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"My_App_Name.sqlite"]; 

     NSError *error; 
     if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) 
     { 
      if ([[NSFileManager defaultManager] copyItemAtPath:defaultStorePath toPath:storePath error:&error]) 
       NSLog(@"Copied starting data to %@", storePath); 
      else 
       NSLog(@"Error copying default DB to %@ (%@)", storePath, error); 
     } 

     NSURL *storeURL = [NSURL fileURLWithPath:storePath]; 

     persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
           [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
     { 

      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     }  

     return persistentStoreCoordinator; 
    }  
} 
0

顯示你的代碼,您可以設置核心數據,尤其是管理對象上下文。如果您正在應用程序包內創建後備存儲,這將在模擬器上成功,但在設備上將失敗。 (作爲可能出錯的一個例子)

+0

核心數據堆棧更新了問題。 – Maverick 2010-10-06 21:47:14