How do you request Core Data to get only one attribute?

Asked

Viewed 113 times

0

I just need to get an attribute from a single row of the table:

 name  | id   | idade  | aprov|repro| sala | 
_______|______|________|______|_____|______| 
John   |  32  |    15  |   A  |     | 155  | 

Following the table above would be the field idade containing the element 15.

How can I do this using Core Data?

In case this app is for iOS.

1 answer

1


To get only the column age you define the method setPropertiesToFetch: of your object NSFetchRequest, something like that:

[request setPropertiesToFetch:[NSArray arrayWithObjects:@"idade", nil]];

And to make the filter, a NSPredicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %d", 32];
[request setPredicate:predicate];

And so performs the rest of your request that will return a single object, according to the id.

  • But this way he will bring me a whole column, in the case of a Nsarray, now that I have looked at it, and rethought it, I need him to bring me specifically a LINE and a COLUMN, to bring only a single element. Got it?

  • Okay, so I added this implementation in my reply.

  • Thanks buddy, I’ll test and I’ll get back to you whether it worked or not!

  • worked, but the information I wanted to pick up is not accessible, because I wanted to access the Primary Key generated by core data, in case it appears in the table as Z_PK. Gave an error saying that it is not available in the table I am using :/

  • The attribute Z_PK is not just any field that you use in Core Data. You do not depend on this value to maintain the consistency of your base, even because the schema can and will change with each update of your application, which is why this value is not accessible. What is the need to get it?

  • My app has 2 tables, and one of them will only have a record, ie a Z_PK only of value 1. Which will be the user record, and the app to fill the second table, will need the user data that are in the first. Got it?

  • But of all user items, the most used will be the ID_REGISTRO field of line 1 in the case

  • Understand that the attribute Z_PK is of internal use of Core Data, so much so that you didn’t raise him in Xcode, you can only see it if you extract the bank Sqlite of your app. So the relationship the way you’re looking to do with this attribute has to be rethought.

  • Yes I already rethought: The user registration will only happen once, so I will only have 1 record. So when I make the request I will only have one record for Nsarray, so it’s only what I want rs :D

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.