For a profile screen, I would like to load the equivalent of a detail page from Core Data without having to load a table first. I have an entity/ table of multiple users in core data. But I'm only interested in pulling the profile data on one of them, the current user. I would like to load that directly without going through the process of loading a table and choosing a selection and using path and index row etc. Once I get the results, I don't want to display them as a row but rather distribute different fields around the page in different elements as you do in a detail page.
Can anyone suggest the best way to do this?
I imagine I need to use a predicate that sorts for the user in question with userid or username. Ordinarily I use NSFetchedResultsController to perform fetches but perhaps there is a way to do a simpler fetch with executefetchrequest. Following code is adapted from something I found on SO. Is it right approach?
- (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;
}