Friday, May 28, 2010

Core Data and Threading

I've had a number of people ask me lately about the best way to handle using Core Data in a multi-threaded application. I began to put together a tutorial and sample code based on Apple's documentation, which advises to pass NSManagedObjectIDs between threads to get around the fact that threads can't share instances of NSManagedObjectContext or NSManagedObject. I've used this approach in several projects and it works.

While googling around doing research, however, I came across a post by Marcus Zarra on the Mac Developer Network.

Marcus knows Core Data really well, having written a book on the topic, so I figured I'd take a look at what Marcus had to say on the matter. I'm glad I did. Marcus illustrates a really great approach in his post that obviates the need to pass ManagedObjectIDs around between the thread in many situations. You still need to do that if you need to pass a specific object between threads, but if you're doing something like loading or creating managed objects in the background to be displayed in a table, this is so much easier and better.

I won't spoil the surprise, but it's a handy tool for your Core Data toolkit. If you have any thought about using Core Data in a multi-threaded application, you should definitely check it out.



7 comments:

Jim said...

At Kobo, we do a lot of CoreData stuff on background threads, using the same method Marcus demonstrates. We have one addition: a wrapper function to get a context appropriate to the current thread. The relevant code is naturally available on github.

The idea is that the app delegate creates the main thread's context at startup and calls StoreManagedObjectContextForCurrentThread() to prevent auto-generation of a second one on the main thread. From this point on, all code on any thread just calls PerThreadManagedObjectContext() to get a cached context unique to its own thread.

Jeff LaMarche said...

Jim:

Thanks for sharing. I was actually thinking about doing something along the same lines but frankly, I'd rather steal yours :)

Jeff

Heath said...

I'm doing a large CoreData search on a background thread, and we shared the objects with the UI thread by using NSManagedObjectContext's NSLocking protocol.

All uses of NSManagedObjectContext occur within a DAO object, and that DAO guarantees that -[NSManagedObjectContext lock] and -[NSManagedObjectContext unlock] is called.

This was easier for us to implement than using separate NSManagedObjectContexts given our existing codebase. I also had a concern (though I never profiled it because we never implemented multi-context) that passing around object IDs would incur new fetch costs. Does anyone know if this is the case?

雅鈴 said...

85cc成人免費觀看 免費視訊影片 情色視訊聊天 免費av18影片 豆豆聊天交友論壇 女優光碟網 免費看性感影片 做愛影片! 偷拍實錄 UT視訊聊天室 女同志愛愛影片免費 色瞇瞇情趣用品網 18禁下載 少女脫衣秀遊戲 3d免費a影城 哪裡看a片 麗的情色網站 色情成人片 桃園一夜情留言板 383影音網 完美性交 玩美情人論壇 波波遊戲美女 動漫色辣妹 人妻 人妻熟女圖片 免費視訊 live173 a片分享區 巨乳美女寫真集 85cc倉井空 微風線上觀看區 ut聊天室 777成人 小魔女貼圖 援交電話 台灣情人網dvd 全裸鋼管秀 日本av免費 洪爺影城討論區 大奶美女圖貼 sex520貼片 情色電影下載 巨乳人妻 性愛光碟 後宮電影 成人圖庫 自拍0204貼圖 性愛貼圖自拍 模特兒走光圖

airshoes said...

I like your blog. Thank you. They are really great .
Some new style Puma Cat shoes is in fashion this year.
The Nike Air Shoes is a best Air Shoes .
Red Shox is good and Cheap Nike Shoes.
If you are a fans of Puma basket,we would offer the good and Cheap Puma Shoes for you .the nike shox torchis best christmas gift now.
Running Shoes
Nike Shox R4
Nike Air Force Ones
The information age is really convenient .

SEO Services Consultants said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels