I am very new to Core Data and have been trying to following many tutorials, but most of them put all of the Core Data methods into AppDelegate.so any one help me please Thanks in advance
- (NSFetchedResultsController *)fetchedResultsController
{
// NSLog(@"Calling fetchedResultsController @ rootviewController");
if (is_Searching && [search_string length])
{
NSManagedObjectContext *moc = [[AppDelegate appdelegate] managedObjectContext_roster];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
inManagedObjectContext:moc];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSMutableArray * predicateArray = [[NSMutableArray alloc]init];
if (is_Searching && [search_string length]) {
NSPredicate *predecate = [NSPredicate predicateWithFormat:@"displayName CONTAINS [c] %@",search_string];
[predicateArray addObject:predecate];
}
if ([[AppDelegate get_update_privacy_Array] count]) {
for (NSString * jids in [AppDelegate get_update_privacy_Array]) {
NSPredicate *predecate_blocked = [NSPredicate predicateWithFormat:@"NOT(nickname CONTAINS [c] %@ OR jidStr CONTAINS %@)" ,jids, jids];
[predicateArray addObject:predecate_blocked];
}
}
NSPredicate *predicate_final = [NSCompoundPredicate andPredicateWithSubpredicates:
predicateArray];
[fetchRequest setPredicate:predicate_final];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:10];
fetchedResultsController_search = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@"sectionNum"
cacheName:nil];
[fetchedResultsController_search setDelegate:self];
NSError *error = nil;
if (![fetchedResultsController_search performFetch:&error])
{
//DDLogError(@"Error performing fetch: %@", error);
}
if (![[fetchedResultsController_search fetchedObjects]count] && ![groupChatArray count]) {
[AppDelegate alertWithTitle:@"Alert" message:@"No contact found!"];
}
return fetchedResultsController_search;
}
else
{
NSManagedObjectContext *moc = [[AppDelegate appdelegate] managedObjectContext_roster];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
inManagedObjectContext:moc];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSMutableArray *predicateArray = [[NSMutableArray alloc]init];
if ([[AppDelegate get_update_privacy_Array] count]) {
for (NSString * jids in [AppDelegate get_update_privacy_Array]) {
NSPredicate *predecate_blocked = [NSPredicate predicateWithFormat:@"NOT(nickname CONTAINS [c] %@ OR jidStr CONTAINS %@)" ,jids, jids];
[predicateArray addObject:predecate_blocked];
}
}
NSPredicate *predicate_final = [NSCompoundPredicate andPredicateWithSubpredicates:
predicateArray];
[fetchRequest setPredicate:predicate_final];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:10];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@"sectionNum"
cacheName:nil];
[fetchedResultsController setDelegate:self];
NSError *error = nil;
if (![fetchedResultsController performFetch:&error])
{
//DDLogError(@"Error performing fetch: %@", error);
}
return fetchedResultsController;
}
}
error iam getting is
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'XMPPUserCoreDataStorageObject''
Aucun commentaire:
Enregistrer un commentaire