I am creating an FBChat using XMPP.I have made separate classes for coredata and fetchedResults.
CoreDataClass:
@implementation CoreDataClass
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init] ;
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Chat" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSURL *storeURL = [[delegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"FacebookChat.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
FetchedControlClass:
@implementation FetchedControllClass
@synthesize fetchedResultsController;
#pragma mark Fetched Results
- (NSFetchedResultsController *)fetchedResultsController
{
CoreDataClass *coreDataObject=[[CoreDataClass alloc]init];
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Conversation" inManagedObjectContext:coreDataObject.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"facebookName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataObject.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"] ;
[fetchRequest release];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[coreDataObject release];
return fetchedResultsController;
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
AlertListVC *alertListObject=[[AlertListVC alloc]initWithNibName:@"AlertListVC" bundle:nil];
[alertListObject.tableView beginUpdates];
[alertListObject release];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
AlertListVC *alertListObject=[[AlertListVC alloc]initWithNibName:@"AlertListVC" bundle:nil];
switch(type) {
case NSFetchedResultsChangeInsert:
[alertListObject.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[alertListObject.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
[alertListObject release];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
AlertListVC *alertListObject=[[AlertListVC alloc]initWithNibName:@"AlertListVC" bundle:nil];
UITableView *tableView = alertListObject.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[alertListObject configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
[alertListObject release];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
AlertListVC *alertListObject=[[AlertListVC alloc]initWithNibName:@"AlertListVC" bundle:nil];
[alertListObject.tableView endUpdates];
[alertListObject release];
}
I have made objects for both classes in the MainChatClass and adding the conversation from coredata to ConversationClass
Conversation *conversation = (Conversation *)[NSEntityDescription
insertNewObjectForEntityForName:@"Conversation"
inManagedObjectContext:coreDataClassObject.managedObjectContext];
I am able to get the fetchedResults but
The issue is:
Coredata methods are getting caleed two times(so file is creating two times).If I am selecting the friend to chat,then the application will crash.I have used zombies for tracking the issue.It is showing some errors as
responsible caller:
1.[MainChatClass tableView:didSelectRowAtIndexPath]
2.[NSFetchedResultsController(Private Methods) _managedObjectContextDidChange]
I have tried many times,but still getting errors with these methods.
If any one having idea for this then please help me.
Thanks in advance
In each of the FRC delegate methods you create a new
alertListObject. That makes no sense. You must send the update messages to your existing table view.The crash occurs because
alertListObject.tableViewisnilfor a newly created view controller.