Core Data fetch with Predicate

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

  1. cpuin macrumors member

    Joined:
    Feb 3, 2013
    #1
    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);
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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

    Joined:
    Feb 3, 2013
    #3
    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 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    "Replace the predicate"?

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

    Joined:
    Mar 2, 2013
    #5
    fault does not mean an error
     
  6. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #6
    +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).
     

Share This Page