我正在尝试在Core Data中轻量迁移SQLite存储.使用Xcode 4.3.1在Lion 10.7.3上工作.
在我的NSPersistentDocument子类(AccountDocument)中,我重写了用于配置持久性存储协调器的方法,以便它为迁移获取正确的选项:
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error
{
NSMutableDictionary *newStoreOptions;
if (storeOptions == nil) {
newStoreOptions = [NSMutableDictionary dictionary];
}
else {
newStoreOptions = [storeOptions mutableCopy];
}
[newStoreOptions setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[newStoreOptions setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
BOOL result = [super configurePersistentStoreCoordinatorForURL:url ofType:fileType modelConfiguration:configuration storeOptions:newStoreOptions error:error];
return result;
}
Run Code Online (Sandbox Code Playgroud)
(感谢Malcolm Crawford的提示:http://homepage.mac.com/mmalc/CocoaExamples/controllers.html)
当我运行应用程序时,它在NSPersistentDocument的实现中失败-managedObjectModel:
* thread #1: tid = 0x2703, 0x00007fff931d9350 libobjc.A.dylib`objc_msgSend_vtable13 + 16, stop reason = EXC_BAD_ACCESS (code=13, …Run Code Online (Sandbox Code Playgroud) 搜索这篇文章的标题后发现它很常见; 的确,我从Xcode那里得到了这个错误.但我似乎无法找到任何修复.我现在在运行程序时看到它,它似乎发生在调用changeCountTokenForSaveOperation期间或之后.它似乎与撤销管理器有关,而不是与我使用核心数据这一事实相关,但我可能错了.
有谁知道是什么导致这个或如何解决它?
我有一个使用NSPersistentDocument的Mac应用程序.我希望在删除对象时收到通知,以使用Core Data保存此信息.所以我正在侦听NSManagedObjectContextObjectsDidChangeNotification.通常在删除对象时立即调用它,但有时仅在保存持久文档之后调用,之前未通知所有已删除的对象.因为我想让用户直接导出所有现有对象(而不是已删除的对象)我必须做什么才能立即收到通知以保存删除信息,或者我是否必须以其他方式使用它?
macos core-data nsnotifications nsmanagedobjectcontext nspersistentdocument
我正在使用iPhone应用程序和使用Core Data的Mac应用程序.
我想让这两个应用程序通过iCloud存储同步他们的数据库.
我已经做了调整的实现的managedObjectContext&persistentStoreCoordinator&添加mergeiCloudChanges-从更新的食谱示例代码:
#pragma mark -
#pragma mark Core Data stack
// this takes the NSPersistentStoreDidImportUbiquitousContentChangesNotification
// and transforms the userInfo dictionary into something that
// -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] can consume
// then it posts a custom notification to let detail views know they might want to refresh.
// The main list view doesn't need that custom notification because the NSFetchedResultsController is
// already listening directly to the NSManagedObjectContext
- (void)mergeiCloudChanges:(NSNotification*)note forContext:(NSManagedObjectContext*)moc {
NSLog(@"merging …Run Code Online (Sandbox Code Playgroud) 我想创建一个使用
这似乎是不可能的。该文件非常清楚地说明
NSPersistentDocument 不支持某些文档行为:文件包装器。[..]
这让我认为在 CoreData 中处理图像的常用方法 -带有“允许外部存储”的二进制数据并将它们保存到不同的位置,将 URL 存储在数据库中 - 不能与 NSPersistentDocument 一起使用。我希望我的用户能够对我的“文件”执行通常的 Finder 操作(复制、移动到外部存储、从外部备份恢复)并且需要我的所有数据都在一个包中。
文件存储的 SQL 版本在保存时会产生通常的三重堆栈 - .sqlite、.sqlite-shm、.sqlite-wal - 作为“文档”是无用的。
有没有我忽略的解决方案?(示例非常稀少;Big Nerd Ranch 示例也没有解决这个问题;Marcus Zarra 和 Objc.io 都没有触及 NSPersistentDocument)。
我现在遇到过两次这个问题:Xcode 4.1,Lion和NSPersistentDocument Core Data项目.
我的程序运行正常,直到我向模型中的一个实体添加了两个"二进制数据"属性.现在,当我运行应用程序并尝试保存文档时,会从内部抛出异常configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:.
上次我开始收到此错误时,就是我更新了项目目标设置中的设置.那时,重置更改的设置没有任何效果,我不得不从备份恢复Xcode项目文件,然后它工作 - 所以它不是代码相关的.
这是抛出异常时的日志:
2011-08-27 17:34:00.867 MyAppName[5690:707] Unsupported store type.
2011-08-27 17:34:00.879 MyAppName[5690:707] (
0 CoreFoundation 0x00007fff8ae0e986 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8e28cd5e objc_exception_throw + 43
2 CoreData 0x00007fff9576776a -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 1530
3 AppKit 0x00007fff9190af88 -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:] + 554
4 MyAppName 0x0000000100001af4 -[MyAppNameDocument configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:] + 244
5 AppKit 0x00007fff91bc6697 -[NSPersistentDocument(NSDeprecatedInternal) _configurePersistentStoreCoordinatorForURL:ofType:error:] + 172
6 AppKit 0x00007fff91909a82 -[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] + 418
7 MyAppName 0x0000000100001c51 -[MyAppNameDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] + 161
8 AppKit 0x00007fff917c5062 -[NSDocument _writeSafelyToURL:ofType:forSaveOperation:forceTemporaryDirectory:error:] …Run Code Online (Sandbox Code Playgroud) 在使用加载现有文档时NSPersistentDocument,作为初始化的一部分,我想准备一些内容:
NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"DocumentRoot"];
NSArray *results = [self.managedObjectContext executeFetchRequest:req error:NULL];
if (results.count) self._docRoot = [results objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
当我将此代码放入时-init,提取请求不会返回任何结果。
在将视图控制器组件从我的NSPersistentDocument子类重构到新的NSWindowController子类时遇到了这个问题。我曾经在中处理过这种初始化-windowControllerDidLoadNib:,但是现在不再调用了。
如果将代码从-init移到,-makeWindowControllers我会得到预期的结果。是否-makeWindowControllers真的准备这样的内容的权利的地方吗?
core-data ×6
macos ×4
cocoa ×2
objective-c ×2
osx-lion ×2
icloud ×1
ios ×1
nsdocument ×1
xcode ×1