0

Hi i have a function which fetches data from coreData. Here is the method

- (NSArray *)fetchRequestWithEntity:(NSEntityDescription *)entity
                      predicate:(NSPredicate *)predicate
                           sort:(NSArray *)sortDescriptors
                          range:(NSRange)range {
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest new];
[request setEntity:entity];
if (predicate != nil) {
    request.predicate = predicate;
}

if (sortDescriptors) {
    request.sortDescriptors = sortDescriptors;
}

if (range.length > 0) {
    request.fetchLimit = range.length;
}

if (range.location > 0) {
   request.fetchOffset = range.location;
}
request.fetchBatchSize = 15;

NSArray *array = [self executeFetchRequest:request error:&error];
if (!error) {
    return array;
}
else {

    NSLOG("Error");
    return nil;
}

}

The program is crashing at this line.

NSArray *array = [self executeFetchRequest:request error:&error];

I checked the value of all the parameters of the method and everything seems to be fine. Does this have anything to do with the "sortDescriptors" parameter of the method?

This is how I am getting NSSortDescriptor

+ (NSArray *)sortDescriptors {
return @[[[NSSortDescriptor alloc] initWithKey:EIKNewsHeadlineAttributeStoryDateTime ascending:NO]];

}

I checked the parameters of the method ,i have a valid entity and sortDescriptor. Value of predicate is nil and my range is (location=0 and length=0). What is causing the problem?

iPhoneDeveloper
  • 958
  • 1
  • 14
  • 23
  • Is `self` a `NSManagedObjectContext` object? Does `sortDescriptors` contains only `NSSortDescriptors` object? How are they created? – Larme Jun 23 '17 at 09:38
  • Yes self here refers to NSManagedObjectContext. I am using the above method in the NSManagedObjectContext+Helper class. An extension of it. – iPhoneDeveloper Jun 23 '17 at 10:00

0 Answers0