我讀過一堆線程,討論到每次運行模擬器時都會改變文檔目錄的路徑,但討論的是關於DeviceID,我同意沒有改變。什麼是變化的ApplicationID,至少使用我找到它的方法,我無法在下一次運行找到它,所以我的SQL數據庫不在那裏。Xcode 6模擬器爲每次運行重命名ApplicationID(不是DeviceID)
這裏是和例子我所看到的:
首先運行: 文件目錄:/用戶/ annewest /庫/開發商/ CoreSimulator /設備/ 9DE1C194-BFB1-47CA-9CA1-77FA89F43960 /數據/容器/數據/應用程序/ 4EFF7925-BB9E-4D9F-BD8C-B5BF44361189/Documents
然後,我停止在Xcode中的應用程序,並再次運行。現在我看到
第二輪: 文件目錄:/用戶/ annewest /庫/開發商/ CoreSimulator /設備/ 9DE1C194-BFB1-47CA-9CA1-77FA89F43960 /數據/集裝箱/數據/應用/ 8409664B-8EF4-4318 -A6D7-EB07E965F0F6/Documents
請注意DEviceID,第一個隨機字符串,是相同的,但ApplicationID已更改。
下面是我用找到的目錄和文件的路徑和創造持久存儲協調代碼:
- (NSString *) documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths lastObject];
NSLog(@"Documents directory%@", documentsDirectory);
return documentsDirectory;
}
- (NSString *) dataStorePath
{
return [[self documentsDirectory] stringByAppendingPathComponent:@"DataStore.sqlite"];
}
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator
{
if (!_persistentStoreCoordinator) {
NSURL *storeURL = [NSURL fileURLWithPath:[self dataStorePath]];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError *error;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Error adding persistent store %@ %@", error, [error userInfo]);
abort();
}
}
return _persistentStoreCoordinator;
}
有人可以用更多的知識比我幫助解決了這一點?
THX