Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cpuin

macrumors member
Original poster
Feb 3, 2013
77
0
i have the following entity:

Contragents

with:

id (int64)
name (string)
city (string)
address (string)

I'm trying to fetch and display in the console all records, but i don't know how to work with predicate:

Code:
// fetch all Contragents
    NSManagedObjectContext *moc = [[NSApp delegate] managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription
                                              entityForName:@"Contragents" inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    
    //
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"(name)"];
    [request setPredicate:predicate];
    
    NSError *error;
    NSArray *array = [moc executeFetchRequest:request error:&error];
    if (array == nil)
    {
        NSLog(@"There is some error :(");
    }
    NSLog(@"array: %@", array);
 
If you want to display everything then you shouldn't have a predicate at all. Predicates are for filtering sets of data, IE, only show city names that start with the letter C.
 
If you want to display everything then you shouldn't have a predicate at all. Predicates are for filtering sets of data, IE, only show city names that start with the letter C.

I see.

But when i replace the Predicate the console returns this:

2013-09-08 13:25:24.431 Faktura[523:303] array: (
"<NSManagedObject: 0x1001a6bb0> (entity: Contragents; id: 0x1001a1ce0 <x-coredata://0514BB73-CED6-4D39-AF64-8A0057495CEC/Contragents/p102> ; data: <fault>)"
)

not the data
 
"Replace the predicate"?

Replace it with what? Show the code you wrote to generate that error.
 
I see.

But when i replace the Predicate the console returns this:

2013-09-08 13:25:24.431 Faktura[523:303] array: (
"<NSManagedObject: 0x1001a6bb0> (entity: Contragents; id: 0x1001a1ce0 <x-coredata://0514BB73-CED6-4D39-AF64-8A0057495CEC/Contragents/p102> ; data: <fault>)"
)

not the data

fault does not mean an error
 
fault does not mean an error

+1

@cpuin: I would suggest going back and reading the Core Data Programming Guide and the Predicate Programming Guide. If you don't have a good handle on Core Data, predicates, expressions, etc, you're going to run into problems every time you need to communicate with the database.

Fetch requests are intimidating the first few times you work with them, but they are simpler to understand when you think of them in terms of SQL (assuming you're familiar with SQL).

SELECT field FROM table WHERE conditions ORDER BY order

field = propertiesToFetch
table = entity
conditions = predicate
order = sortDescriptors

An example predicate would be:
NSPredicate *predicate = [NSPredicate predicateWithFormat:mad:"name contains[cd] %@", @"cpuin"];

PS: A fault refers to a placeholder object whose properties haven't been loaded from the database. When you access persistent (stored) properties, the fault is fired (meaning the properties are retrieved from the database).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.