gpt4 book ai didi

objective-c - 核心数据 "Connection Invalid"

转载 作者:行者123 更新时间:2023-11-28 20:39:11 25 4
gpt4 key购买 nike

所以这很糟糕。尝试使用来自 NSDictionaries 的信息填充核心数据数据库。

我使用下面的代码打开一个 UIManaged 文档,完成处理程序 block 然后调用一个方法,该方法从 API 递归填充字典并为每个字典添加一个实体。

数据库文件创建得很好,字典填充正常,实体对象也填充了,但由于某种原因信息没有写入数据库,虽然它有时有效,但我不确定如果有什么不同的话。

我在控制台中收到一条消息:

“NSFileCoordinator:发出令人惊讶的服务器错误信号。详细信息:连接无效”

每当方法 openWithCompletionHandler 或 saveToURL:forSaveOperation:withCompletionHandler 运行时。

我无法理解为什么下面的代码无法运行,因为它是从斯坦福 iphone 类(class)中剪切和粘贴的。

我试过从模拟器中删除该应用程序,但没有任何区别。这让我头疼了几个小时,感谢任何帮助,谢谢。

+ (void)populateDatabase
{
// Get URL -> "<Documents Directory>/<TrailerDB>"
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

url = [url URLByAppendingPathComponent:@"TrailerDB"];

UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:url];

// If document exists on disk...

if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]])
{
[doc saveToURL:url
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success)
{

if (success) [self fillDatabase:doc.managedObjectContext];

if (!success) NSLog(@"couldn’t open document at %@", url);
}];

} else {

if (doc.documentState == UIDocumentStateClosed) {
// exists on disk, but we need to open it
[doc openWithCompletionHandler:^(BOOL success)
{

if (success) [self fillDatabase:doc.managedObjectContext];

if (!success) NSLog(@"couldn’t open document at %@", url);
}];
} else if (doc.documentState == UIDocumentStateNormal)
{
[self fillDatabase:doc.managedObjectContext];
}
}
}

最佳答案

填充数据库后,尝试调用此方法:

[doc saveToURL:doc.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success)
{
if (success == NO)
{
NSLog(@"Save operation failed!");
}
}];

关于objective-c - 核心数据 "Connection Invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9301747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com