Core Data fetch with Predicate

Discussion in 'Mac Programming' started by cpuin, Sep 7, 2013.

  1. cpuin macrumors member

    Feb 3, 2013
    i have the following entity:



    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:

    // 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:
        [request setPredicate:predicate];
        NSError *error;
        NSArray *array = [moc executeFetchRequest:request error:&error];
        if (array == nil)
            NSLog(@"There is some error :(");
        NSLog(@"array: %@", array);
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    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.
  3. cpuin thread starter macrumors member

    Feb 3, 2013
    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
  4. ArtOfWarfare macrumors G3


    Nov 26, 2007
    "Replace the predicate"?

    Replace it with what? Show the code you wrote to generate that error.
  5. Sonnestah macrumors regular

    Mar 2, 2013
    fault does not mean an error
  6. lucasgladding macrumors 6502

    Feb 16, 2007
    Waterloo, Ontario

    @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).

Share This Page