gpt4 book ai didi

ios - executefetchrequest 导致崩溃

转载 作者:行者123 更新时间:2023-11-29 03:17:40 25 4
gpt4 key购买 nike

我正在尝试在 iOS 7 应用程序中实现 Core Data(并且之前已成功完成)。但是,当我执行executeFetchRequest 方法时,应用程序崩溃了。下面添加了最相关的代码:

#import "JTHuntingSeasonDB.h"

@implementation JTHuntingSeasonDB

- (id)init {
self = [super init];
if (self) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];

NSString *documentName = @"JTHuntingDB";
NSURL *url = [documentsDirectory URLByAppendingPathComponent:documentName];

document = [[UIManagedDocument alloc] initWithFileURL:url];


BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]];

if (fileExists) {
[document openWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"Document opened successfully");
self.allSpecies = [self getSpecies];
self.speciesToday = [self getSpeciesToday];
} else NSLog(@"Failed to open document");
}];
} else {
[document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Document created successfully");
self.allSpecies = [self getSpecies];
self.speciesToday = [self getSpeciesToday];
} else NSLog(@"Failed to create document, path: %@", url);
}];
}
}

return self;
}

#pragma mark - Core Data

- (NSArray *)getSpecies {
if (document.documentState == UIDocumentStateNormal) {
context = document.managedObjectContext;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *country = [defaults stringForKey:@"Country"];
NSString *subregion = [defaults stringForKey:@"Subregion"];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"HuntingDates"];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedStandardCompare:)];
request.fetchBatchSize = 150;
request.fetchLimit = 150;
request.sortDescriptors = @[sortDescriptor];

if (subregion.length > 0) {
request.predicate = [NSPredicate predicateWithFormat:@"(country IN %@) AND (subregion IN %@)", country, subregion];
} else {
request.predicate = [NSPredicate predicateWithFormat:@"country IN %@", country];
}

NSError *error;
return [context executeFetchRequest:request error:&error];
}

return nil;
}

线路

return [context executeFetchRequest:request error:&error];

导致应用程序崩溃。这是我迄今为止所做和发现的:

  • 模拟器:已删除应用并重置内容和设置
  • Xcode:清理(构建文件夹)
  • “上下文”不为零
  • NSLog 打印出“文档已创建/打开成功”
  • 当应用程序崩溃时,控制台中只会打印“lldb”

编辑:在听从 Wain 的建议后,我关闭了异常断点,得到了一条更具描述性的错误消息,指出谓词无效。通过用 CONTAINS 替换谓词中的 IN 并更改变量“country”以使其不再返回 nil 来解决问题。

最佳答案

我猜这是你的谓词,因为 IN 是一个集合运算符,但你传递的是字符串(应该是数组/集合)。

如果您使用字符串,则应在谓词中使用 CONTAINS

关于ios - executefetchrequest 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21491139/

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