1
votes

How to convert dictionary data to NSManagedObject data? I used propertiesToFetch, and it returns dictionary data. But I want NSManagedObject data. please guide me

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:catalogID, pageID, nil]];
[fetch setResultType:NSDictionaryResultType];
[fetch setReturnsDistinctResults : YES];
NSError* error = nil;
self.resultPageArray = [context executeFetchRequest:fetch error:&error];

When I changed the type to NSManagedObjectResultType, then I got non-distinct data. I read that while using propertiesToFetch it saves the data in dictionary format. But I want to get the distinct data and save it into the NSManagedObjects

2
The question is a little vague. Can you post the code you tried and let us know where you got stuck? Mayle this will clarify the question. - Cristik
Don't ask for the dictionary representation if you don't want it. - Avi
@avi i also tried that, but after that i got all the data which is not distinct as well - Aly
The distinctness of the results does not change based on the datatype used to store them. You need to fix your predicate. - Avi
Both propertiesToFetch and resultType affect only received data (get) it does not affect how the data is saved (set) - vadian

2 Answers

0
votes

Try This,

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:catalogID, pageID, nil]];
[fetch setResultType:NSManagedObjectResultType];
[fetch setReturnsDistinctResults : YES];
NSError* error = nil;
self.resultPageArray = [context executeFetchRequest:fetch error:&error];

My code :

NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BookInfo"
                                          inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPropertiesToFetch:@[catalogID, pageID]];

[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"(bookId == %@)",strId]];

NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

if( fetchedItems.count > 0 ) {
    BookInfo *bInfo =  [fetchedItems lastObject];
    NSLog(@"%@",bInfo.bookName);
}
0
votes

I have solved the issue. I was fetching the data of NSManagedObjects. After that I saved that data to the other array without repetition. And return the new array.

self.resultPageArray = [context executeFetchRequest:fetch error:&error];            
NSMutableArray *resultantFinalArr = [[NSMutableArray alloc] init];
for (int i = 0; i<resultPageArray.count; i++)
{
    catalogPageDataModel *atIndexI = [resultPageArray objectAtIndex:i];
    BOOL find = NO;
    for (catalogPageDataModel* catalogPageDataModel in resultantFinalArr)
    {
        if (catalogPageDataModel.pageid.intValue == atIndexI.pageid.intValue)
        {
            find = YES;
            break;
        }
    }
    if (!find)
        [resultantFinalArr addObject:atIndexI];
}
            NSLog(@"result array count %lu",(unsigned long)resultantFinalArr.count);
NSLog (@"names: %@",resultantFinalArr);
return resultantFinalArr;