IOS/Objective-C: 从 Core Data 加载一条记录而不先加载 table
IOS/Objective-C: Load one record from Core Data without loading table first
对于配置文件屏幕,我想从 Core Data 加载等效的详细信息页面,而不必先加载 table。我在核心数据中有多个用户的实体/ table。但我只对提取其中之一(当前用户)的个人资料数据感兴趣。我想直接加载它,而无需经历加载 table 和选择一个选择以及使用路径和索引行等过程。一旦我得到结果,我不想将它们显示为一行但是而是像在详细信息页面中那样在页面的不同元素中分布不同的字段。
任何人都可以建议最好的方法吗?
我想我需要使用一个谓词来根据用户标识或用户名对有问题的用户进行排序。通常我使用 NSFetchedResultsController 来执行提取,但也许有一种方法可以使用 executefetchrequest 进行更简单的提取。以下代码改编自我在 SO 上找到的内容。这是正确的做法吗?
- (User *)userInfo:(NSDictionary *)usersList
inManagedObjectContext:(NSManagedObjectContext *)context
{
User *user = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Users"];
request.predicate = [NSPredicate predicateWithFormat:@"username = bob"];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"last" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches || ([matches count] > 1)) {
// handle error
} else {
user = [matches lastObject];
}
return user;
}
你的代码有什么问题?它没有什么作用?
根据您的描述,您提供的代码完全符合您的要求。
至于您关于加载 table 的评论,您从未加载过 table。 Core Data是一种对象模型设计。即使你将它配置为持久化到 SQLite,你仍然永远不会 "loading a table"。您正在加载一组对象。
像对待 ORM 一样对待核心数据将会在以后给您带来更多的设计问题。它是对象图而不是 ORM。
The immediate error is I don't have the syntax right for calling the method. I am trying to call it in viewDidLoad with [self userInfo:?? inManagedObjectContext:(NSManagedObjectContext *)context but I don't know what to include as ??. However, before going crazy trying to get it to work, I wanted to find out if this was the right approach. Glad to hear that it is. Can you suggest how to call the method? I am fuzzy on what the dictionary is we are really calling here.
方法是你自己定义的。你想要那本字典包含什么?它不是核心数据的一部分 API 它是您的视图控制器的一部分。
该方法中的代码相当标准公平。针对核心数据的简单提取。
如果这个方法不是你写的,那我建议你问问写的人。
仅供参考,谓词中没有使用该词典中的任何内容。这会是家庭作业吗?
对于配置文件屏幕,我想从 Core Data 加载等效的详细信息页面,而不必先加载 table。我在核心数据中有多个用户的实体/ table。但我只对提取其中之一(当前用户)的个人资料数据感兴趣。我想直接加载它,而无需经历加载 table 和选择一个选择以及使用路径和索引行等过程。一旦我得到结果,我不想将它们显示为一行但是而是像在详细信息页面中那样在页面的不同元素中分布不同的字段。
任何人都可以建议最好的方法吗?
我想我需要使用一个谓词来根据用户标识或用户名对有问题的用户进行排序。通常我使用 NSFetchedResultsController 来执行提取,但也许有一种方法可以使用 executefetchrequest 进行更简单的提取。以下代码改编自我在 SO 上找到的内容。这是正确的做法吗?
- (User *)userInfo:(NSDictionary *)usersList
inManagedObjectContext:(NSManagedObjectContext *)context
{
User *user = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Users"];
request.predicate = [NSPredicate predicateWithFormat:@"username = bob"];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"last" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches || ([matches count] > 1)) {
// handle error
} else {
user = [matches lastObject];
}
return user;
}
你的代码有什么问题?它没有什么作用?
根据您的描述,您提供的代码完全符合您的要求。
至于您关于加载 table 的评论,您从未加载过 table。 Core Data是一种对象模型设计。即使你将它配置为持久化到 SQLite,你仍然永远不会 "loading a table"。您正在加载一组对象。
像对待 ORM 一样对待核心数据将会在以后给您带来更多的设计问题。它是对象图而不是 ORM。
The immediate error is I don't have the syntax right for calling the method. I am trying to call it in viewDidLoad with [self userInfo:?? inManagedObjectContext:(NSManagedObjectContext *)context but I don't know what to include as ??. However, before going crazy trying to get it to work, I wanted to find out if this was the right approach. Glad to hear that it is. Can you suggest how to call the method? I am fuzzy on what the dictionary is we are really calling here.
方法是你自己定义的。你想要那本字典包含什么?它不是核心数据的一部分 API 它是您的视图控制器的一部分。
该方法中的代码相当标准公平。针对核心数据的简单提取。
如果这个方法不是你写的,那我建议你问问写的人。
仅供参考,谓词中没有使用该词典中的任何内容。这会是家庭作业吗?