Tirth
Tirth

Reputation: 7789

Fetch distinct data from Core data alongwith descending order?

I creating chat app in which i save data in core data. I using following entity,

enter image description here

I want to fetch data all unique customerno and that data will be descending order of messagedate also contain latest messagetext which send from other cmid. I want to show list like facebook chat message list and smiler view. I trying following code ,

-(NSArray *)getAllInstanceMessages{
    NSError *error = nil;
    NSFetchRequest * req = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"IMDetails" inManagedObjectContext:theManageContext];
    [req setPredicate:[NSPredicate predicateWithFormat:@"cmid=%@", theCmID]];
    NSDictionary *entityProperties = [entity propertiesByName];
    [req setEntity:entity];
    [req setReturnsDistinctResults:YES];
    [req setResultType:NSDictionaryResultType];
    [req setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"customerno"]]];
    [req setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"messagedate" ascending:NO]]];
    NSArray *result = [theManageContext executeFetchRequest:req error:&error];

    if(error){
        NSLog(@"%s, %@", __FUNCTION__, [error localizedDescription]);
        return nil;
    }
    return result;
}

the above code given my following output,

Printing description of result:
<_PFArray 0xa3cda70>(
{
    customerno = CN00001;
    customerno = CN00002;
    customerno = CN00003;
}
)

I want complete data along with this unique numbers, i mean array of dictionary/ManagedObject containing unique data suppose in above CN0001 number but it doesn't contain other attributes values.

what i do wrong in code?

Upvotes: 2

Views: 610

Answers (2)

Anand Mishra
Anand Mishra

Reputation: 1103

you need to set all attribute of table which you want to get in setPropertiesToFetch. Like

[req setPropertiesToFetch:[NSArray arrayWithObjects:[entityProperties objectForKey:@"customerno"],[entityProperties objectForKey:@"messagetext"],nil]];

This will return NSDictionary with both value.

Upvotes: 0

Kamaros
Kamaros

Reputation: 4566

According to the NSFetchRequest Class Reference documentation for the setPropertiesToFetch method: "This value is only used if resultType is set to NSDictionaryResultType."

Upvotes: 1

Related Questions