PDA

View Full Version : NSFectchedResultsController's delegate not firing on insert




simerjots
Jul 18, 2011, 11:42 AM
Hey All,

I have 2 NSFectchedResultsController for 2 Entities which populates the tableView fine. If I remove an object, it is removed fine. If I add an object to the first NSFectchedResultsController, it works fine, but does not work fine with the second NSFectchedResultsController.
The object is being added NSManagedContext in the background thread for both entities. The are being updated deleted and inserted on the same background thread for both entities.
So I am really confused to why the insert/update don't work on the second NSFetchController.
thanks for your help


MyMetaDataRegistry *myMetaDataRegistery = [[MyMetaDataRegistry alloc]init];

_fetchedResultsController = [[myMetaDataRegistery fetchedResultsController:@"-1" delegate:self ]retain];
[_fetchedResultsController performFetch:nil];
NSError *error = nil;

_fetchedResultsControllerDocs = [[myMetaDataRegistery fetchedResultsControllerDocs:@"16"delegate:self] retain];
[_fetchedResultsControllerDocs performFetch:&error];


/////THEN THE MyMetaDataRegistry Class

- (NSFetchedResultsController*)fetchedResultsController:(NSString*)parentID delegate:(id)delegate
{
NSLog(@"%@",@"fetchedResultsController");
if(_fetchedResultsController !=nil)
return _fetchedResultsController;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Folders" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ParentID = -1"];
[fetchRequest setPredicate:predicate];


NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"Title" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];



[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
theFetchedResultsController.delegate = delegate;
self.fetchedResultsController = theFetchedResultsController;
//self.fetchedResultsController.delegate = fetchDelegate;

[sort release];
[fetchRequest release];
[theFetchedResultsController release];

return self.fetchedResultsController;
}

- (NSFetchedResultsController*)fetchedResultsControllerDocs:(NSString*)parentID delegate:(id)delegate
{
NSLog(@"%@",@"fetchedResultsControllerDocs");
if(_fetchedResultsControllerDocs !=nil)
return _fetchedResultsControllerDocs;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Documents" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ParentID = %@ and Status=2",parentID];
[fetchRequest setPredicate:predicate];


NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"Title" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[NSFetchedResultsController deleteCacheWithName:@"DocRoot"];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"DocRoot"];
theFetchedResultsController.delegate = delegate;
self.fetchedResultsControllerDocs = theFetchedResultsController;
// self.fetchedResultsControllerDocs.delegate = fetchDelegate;

[sort release];
[fetchRequest release];
[theFetchedResultsController release];

return self.fetchedResultsControllerDocs;
}


This is the background thread, can you see if you anything

-(NSMutableArray*) syncFiles:(NSArray *)files
{
NSManagedObjectContext *context = syncManagedObjectContext_;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Documents" inManagedObjectContext:context]];
//Intiate the fetchRequest to check if the record exists, keep the result in memory
NSMutableArray *docsInDB = [[NSMutableArray alloc]initWithArray:[context executeFetchRequest:fetchRequest error:nil]];
NSMutableArray *docsInSync = [[NSMutableArray alloc]init];
NSMutableArray *filesToDownload = [[[NSMutableArray alloc]init]autorelease];

for(NSDictionary *file in files)
{
NSDictionary *fileInfo = [file objectForKey:@"node"];
NSString *vID = [fileInfo objectForKey:@"Vid"];
NSString *ServerID = [fileInfo objectForKey:@"Nid"];
NSString *parent = [fileInfo objectForKey:@"Term ID"];
NSString *title = [fileInfo objectForKey:@"title"];
int docType;
NSString *image = @"4_page_level.png";
if([[fileInfo objectForKey:@"Presentation Type"] isEqualToString:@"PDF"])
docType = kDocument;
else if([[fileInfo objectForKey:@"Presentation Type"] isEqualToString:@"Video"])
{
docType = kVideo;
image = @"video.png";
}
if(docsInDB == nil)
{

}
__block BOOL _newObect = YES;
//loop through array in memory to check if the folder exists
[docsInDB enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
Documents *d = object;
if([[[d DocumentID] stringValue]isEqualToString:ServerID])
{
_newObect = NO;
[docsInSync addObject:d];
if(![[[d VID] stringValue]isEqualToString:vID])
{
[d setStatus:[NSNumber numberWithInt:kDownloadPending]];
[d setThumb:image];
[d setType:[NSNumber numberWithInt:docType]];
[d setTitle:title];
[d setVID:[NSNumber numberWithInt:[vID intValue]]];
[d setParentID:[NSNumber numberWithInt:[parent intValue]]];
[filesToDownload addObject:fileInfo];
}
else if([[d Status]intValue] == kDownloadPending)
[filesToDownload addObject:fileInfo];
}
d = nil;
}];
if(_newObect)
{
// NSLog(@"%@",[NSNumber numberWithInt:[ServerID intValue]]);
Documents *_newDoc = [NSEntityDescription insertNewObjectForEntityForName:@"Documents" inManagedObjectContext:context];
[_newDoc setDocumentID:[NSNumber numberWithInt:[ServerID intValue]]];
[_newDoc setStatus:[NSNumber numberWithInt:kDownloadPending]];
[_newDoc setThumb:image];
[_newDoc setType:[NSNumber numberWithInt:docType]];
[_newDoc setTitle:title];
[_newDoc setVID:[NSNumber numberWithInt:[vID intValue]]];
[_newDoc setParentID:[NSNumber numberWithInt:[parent intValue]]];
[context insertObject:_newDoc];
[docsInSync addObject:_newDoc];
[filesToDownload addObject:fileInfo];
_newDoc = nil;
}



}

//look at the synFolder comments exact same feature
[docsInSync enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
Documents *document = object;
[docsInDB removeObject:document];
document= nil;

}];

if(docsInDB != nil)
{
[docsInDB enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
Documents *document = object;

NSFileManager *nsfm = [[NSFileManager defaultManager]init];
NSString *docDir = [NSString stringWithFormat:@"%@/%@",[self documentPath],[document DocumentID]];
BOOL isDir2 = NO;
if([nsfm fileExistsAtPath:docDir isDirectory:&isDir2] && isDir2)
{
[nsfm removeItemAtPath:docDir error:nil];
NSLog(@"Dir deleted");
}
[nsfm release];
[context deleteObject:document];
}];
}
[docsInSync release];
[fetchRequest release];
[docsInDB release];
[self saveContext:context];
return filesToDownload;
}
- (void)updateStatus:(NSNumber*)DocumentID
{
NSManagedObjectContext *context = syncManagedObjectContext_;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Documents" inManagedObjectContext:context]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DocumentID = %@", DocumentID];
[fetchRequest setPredicate:predicate];
NSArray *docs = [context executeFetchRequest:fetchRequest error:nil];

Documents *document = [docs objectAtIndex:0];
[document setStatus:[NSNumber numberWithInt:kDownloadComplete]];
[context updatedObjects];
[self saveContext:context];
[fetchRequest release];


}
- (void)saveContext:(NSManagedObjectContext*)managedObjectContext
{
NSLog(@"saving context");
NSError *error = nil;
if (syncManagedObjectContext_ != nil)
{
if ([syncManagedObjectContext_ hasChanges] &&
![syncManagedObjectContext_ save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}


[[NSNotificationCenter defaultCenter] addObserver:appDelegate selector:@selector(managedObjectContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:syncManagedObjectContext_];



}
}