1

I've read through the materials regarding core data and threading and understand the principles of a separate MOC for each thread. My question is, what's the best way to dynamically determine whether to use a different MOC or the main one. I have some methods that are sometimes called on the main thread, sometimes in background. Is dynamically detecting thread not recommended or is it okay? Any pitfalls? Or do people just write separate methods for the background processes?

Some additional detail...i have a refresh process that performs a bunch of updates off the main thread (so not to lock the UI while user is waiting) using a simple performSelectorInBackground. This process moves thru steps serially so i dont have to worry about multiple things accessing DB on THIS thread, obviously the trick is keeping the main and background safe. I have implemented using a separate context and merging in other places, but i recently rearchitected and am now using methods in the background i wasnt before. So i wanted to rewrite those, use the separate context, but sometimes ill be hitting them on the main thread and can access main MOC just fine.

4

3 に答える 3

1

NSManagedObjectContextスレッドごとに新しいが必要になりNSManagedObject、そのスレッドの新しい MOC から の新しいバージョンを作成する必要があります。そのアプローチの長所/短所に関する@sergioの回答を読んでください。

メインスレッドにいるかどうかを確認するには、[NSThread isMainThread]そのように使用して決定を下すことができます。または、新しいスレッドをスピンアップして CoreData を処理する場合は、新しい MOC も作成します。

于 2013-10-04T17:51:08.120 に答える