gpt4 book ai didi

ios - Core Data Managed Object 无法保存上下文(错误 134030)

转载 作者:行者123 更新时间:2023-11-29 01:31:21 45 4
gpt4 key购买 nike

当尝试在持久存储协调器中保存具有以下配置的托管对象时:

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ParallelPhotosModel.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

我收到以下错误:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "An error occurred while saving." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite, NSAffectedStoresErrorKey=( " (URL: file:///var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite)" ), NSUnderlyingError=0x127335c50 {Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist." UserInfo={NSUnderlyingError=0x12732a670 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite}}}}}, { NSAffectedStoresErrorKey = ( " (URL: file:///var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite)" ); NSFilePath = "/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite"; NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=4 \"The file doesn\U2019t exist.\" UserInfo={NSUnderlyingError=0x12732a670 {Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite}}}"; }

这里的潜在问题是什么,我该如何避免它?我在某个地方看到,尝试在应用程序包中保存时会弹出类似的错误,但我使用的 storeURL 取自教程(即文档目录)。

更新:

更具体地说,我粘贴了 NSManagedObjectContext 应该允许我保存但不允许保存的位置:

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

for (DBMetadata *file in metadataContents) {
NSString *photo_info = [file photo_info];
if (photo_info != nil) {
NSNumber *day_taken = [self extractDayFromString: photo_info];
NSNumber *month_taken = [self extractMonthFromString: photo_info];
NSNumber *year_taken = [self extractYearFromString: photo_info];
NSString *db_path = [file path];
NSManagedObject *parallelPhoto = [NSEntityDescription
insertNewObjectForEntityForName:@"ParallelPhotos"
inManagedObjectContext:context];
[parallelPhoto setValue:db_path forKey:@"pathInDropbox"];
[parallelPhoto setValue:day_taken forKey:@"day"];
[parallelPhoto setValue:month_taken forKey:@"month"];
[parallelPhoto setValue:year_taken forKey:@"year"];
NSLog(@"ADDED TO DB: %@ %@ %@", day_taken, month_taken, year_taken);
}
}
NSError *error = nil;
if(![context save:&error]){
NSLog(@"error: %@", error);

最佳答案

在将 URL 添加到持久存储之前,您可能需要确保该目录存在。

[[NSFileManager defaultManager] createDirectoryAtURL:[storeURL URLByDeletingLastPathComponent]
withIntermediateDirectories:YES
attributes:nil
error:NULL];

实际上,您可能需要在返回值之前将此代码添加到您的 applicationDocumentsDirectory 方法中。如果您这样做,请确保正确处理潜在的错误。

编辑

在该行上放置断点

NSManagedObjectContext *context = [appDelegate managedObjectContext];

并逐步完成该操作。检查 MOC 和 PSC 的属性。您还应该检查持久存储所持有的 URL。然后您应该检查文件系统以查看该目录中的内容。

如果这些都不起作用,请在最初创建 MOC/PSC 的位置放置一个断点,并确保它已正确创建。检查文件系统以确保存储存在。

然后,在文件和/或文件系统上安装一个观察器,以便在文件发生更改时得到通知,并查看是否可以捕获正在删除或移动的文件。

关于ios - Core Data Managed Object 无法保存上下文(错误 134030),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33425905/

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