2

Im currently having a problem with my CoreData implementation. I try to query the number of objects with certain properties. I therefore execute the following code

NSUInteger itemsCount = [managedObjectContext countForFetchRequest:fetchRequest error:NULL];
NSLog(@"%i",(int)itemsCount);

So far so good. Everything works if i work with a fresh MOC where no data has ever been saved. The problem occures when i do the following:

  • I save the whole MOC
  • Delete one of those objects matching the fetch criteria from the MOC (without saving!)
  • Requery the Number of objects matching the criteria

The number of objects does not change although i deleted one of the objects. I also tried the following query ...

fetchRequest.resultType = NSCountResultType;
NSArray* array = [managedObjectContext executeFetchRequest:fetchRequest error:&fetchError];
NSLog(@"%i",[[array firstObject] intValue]);

... with the same result.

Then I tried querying the objects directly ...

fetchRequest.resultType = NSManagedObjectResultType;
array = [managedObjectContext executeFetchRequest:fetchRequest error:&fetchError];
NSLog(@"%i",(int)[array count]);

... tada: Here it works. The array does not contain the deleted object. When I set fetchRequest.includesPendingChanges = NO, i of course get all objects because i did not save yet. The property has no effect ob the first two queries though because they show too many objects in the first place.

So my question is, what am i doing wrong and why can i not obtain the right amount of objects once i saved them to the persistence store. Do i have to reset the MOC after saving?

--------------------------------------------------------------------------------

Update

I searched further for similar problems and found some interesting posts. Fetching objects from the MOC means also querying the persistence store. Since I did not save my changes immediately, I do or don't fetch deleted and newly inserted objects respectively. But strangely this only applies for queries involving dictionary result types or count fetches. When querying the objects itself i retrieve the correct number of objects including newly inserted and without the deleted ones.

Now I am wondering on how to deal with my problem. I have a table view which smoothly works with a NSFetchedResultsController (NSFRC). I do not have to save any changes since the controller handles all the updates and notifies me. But as soon as i insert or delete data and the NSFRC notifies me, i want to do further calculations to update my other views. Those calculations include average and number of objects with certain attribute etc. Right now I update those view as soon as an insert or delete delegate method is being called. But those queries wont return the correct values. Calling the following before querying has no effect on those queries.

[moc processPendingChanges]
fetch.includePendingChanges = YES

Apparently my approach is lacking something. How do i fetch my needed values correctly ? Since my NSFRC obviously keeps track of deleted and inserted values im curious if it would help to create a second NSFRC just to fetch the averages and so on? Is it even capable of that?

Thanks!

Community
  • 1
  • 1
Jan
  • 305
  • 3
  • 10
  • It might help to call `processPendingChanges` before checking the number again. – Tom Harrington Aug 11 '15 at 18:58
  • Thanks for your hint but this does unfortunately not solve the problem. I can see the delted object in the "deletedObjects" array of the MOC, so it seems to be processed. Somehow the count fetches seem to ignore the deleted flag and just fetch them, whereby the normal fetch only retrieve undeleted objects. – Jan Aug 12 '15 at 02:35
  • See my updated question! – Jan Aug 13 '15 at 17:14
  • I'm facing the same issue, do you remember if you found a solution ? – Pierre Mardon Oct 27 '17 at 14:03

0 Answers0