Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jeanlain

macrumors 68020
Original poster
Mar 14, 2009
2,443
942
Hi,
Google didn't provide a clear answer on this, but I measured that the equivalent operation on a managed object context takes quite a bit longer when it occurs on a background thread.
The same block executed with -performBlock takes about 75% longer than with -performBlockAndWait. I noticed this for different tasks in my app.

I don't know much about concurrency programming, so I may be stating the obvious here.
If, on the other hand, this is not expected behaviour, I can provide more details.
 

jeanlain

macrumors 68020
Original poster
Mar 14, 2009
2,443
942
I should have phrased it differently. I know what concurrency programming entails is, but I'm not much experienced with it. I've read that document (as a matter of fact, I had dowloaded it from Apple when it was still a pdf back in 2012.)

Anyway, I found what the issue was. I was using a child context of the application's view context. Retrieving objects with this child context make SQLite operations occur on the main thread, while my code block operates on another.
Using a context generated with -newBackgroundContext of NSPersistentContainer did not cause this, and the operation is much quicker.
 
  • Like
Reactions: oleglysiak

Kenny Smith

Suspended
Dec 6, 2022
5
2
Yes, it is possible for Core Data operations to be slower on a private queue, as you have experienced. When you use a private queue, you are performing Core Data operations on a background thread, which can lead to slower performance compared to performing the same operations on the main thread.
The reason for this slower performance is the overhead associated with managing concurrency and thread safety in Core Data, which is necessary to ensure data consistency and integrity. When performing Core Data operations on a private queue, Core Data has to ensure that the managed object context is properly locked and synchronized, which can add additional overhead and slow down the operation.
In addition, using the -perform block method with a private queue can further increase the overhead and slow down operations, as it requires additional coordination and synchronization between the main thread and the background thread.
In general, it is recommended to keep Core Data operations as short as possible and to perform them on the main thread whenever possible, as this will help to minimize the impact on app performance. However, in some cases, it may be necessary to perform Core Data operations on a private queue, in which case it is important to carefully measure performance and optimize the operations as much as possible to minimize the impact on app performance.
I hope this will help you.
 
  • Like
Reactions: jeanlain
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.