CoreData(マルチスレッドの処理 )
CoreDataを扱う場合, main-threadのみでNSManagedObejctContextを利用している場合は, Appleのサンプルどおり, AppDelegateでSingleton実装でかまいません。しかし, NSOperationQueueや, dispatch_asyncなどのバックグラウンド処理でこのSingletonを呼び出すと, NSManagedObjectの何かしらの操作で落ちます。
例)
[_PFManagedObjectReferenceQueue _processReferenceQueue:]
しかも動作がぶつかったときだけ落ちるというなんとも見つけにくいものです。
下のReference通り, NSManagedObjectContextを新たに用意して, NSPersistentStoreCoordinatorはsingletonの分と共有します。さらに, backgroundで変更した結果を, main-threadに伝えるためのnotification, NSManagedObjectContextDidSaveNotificationを受け取ります。これは, 新たなcontext がsabveメソッドを発行したときに呼び出される, Notificationです。
最後に, main-threadへ統合するメソッドを実装します。
結局のところ, main-threadで利用していたSingletonのものはそのままキープです。コードの変更の必要はありません。バックグラウンド処理の部分のみ変更すればよいです。
サンプルコード(省略あり)
@interface BackgroundJob() @property(nonatomic, readwrite, strong)NSManagedObjectContext *context; @end @implementation BackgroundJob -(void)initThreadContext { self.context = [[NSManagedObjectContext alloc] init]; [self.context setPersistentStoreCoordinator:[AppDelegate appdelegate].persistentStoreCoordinator]; NSNotificationCenter *notify = [NSNotificationCenter defaultCenter]; [notify addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:self.context]; // Something to do } - (void)mergeChanges:(NSNotification*)notification { [[[AppDelegate appdelegate] managedObjectContext] performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:YES]; } @end
initThreadContextを実行した後, 新たなcontextを使って処理を書きます。
persistentStoreCoordinator はAppDelegateで実装しているものです(Singleton)。
mergeChangesというメソッドで, main-threadとの統合を書きます, 同じクラス内であればこのように実装すれば対策は十分です。
Ref
マルチスレッドでCore Dataを利用する場合にはスレッドごとにManaged Object Contextを用意する
StackOverflow
Greetings! Very useful advicxe within this article!
It’s the little changes that will make the largest changes.
Many thanks for sharing!
It’s hard to find educated people for this subject, however, you seem like you know
what you’re talking about! Thanks
I am regular visitor, how are you everybody?
This paragraph posted at this site is really fastidious.
What’s up, after reading this amazing article i am also glad to share my knowledge here with colleagues.
I have read so many articles or reviews on the topic
of the blogger lovers however this article is genuinely
a good post, keep it up.
I am sure this paragraph has touhched all the internet viewers, its really really nice pieece of writing on building upp neew web site.
I every time spent my half an hour to read this web site’s content daily along with a mug of coffee.
Aw, this was an extremely nice post. Taking a few minutes and actual effort to generate a great
article… but what can I say… I hesitate a lot and
don’t manage to get nearly anything done.
This post provides clear idea in favor of
the new viewers of blogging, that really how to do blogging.
Wonderful blog! I found it while surfing around on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers
This is a topic that is close to my heart…
Take care! Where are your contact details though?
hello there and thank you for your info – I’ve certainly picked up anything new from right
here. I did however expertise several technical issues
using this website, since I experienced to reload the website lots of times previous to
I could get it to load properly. I had been wondering
if your hosting is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and could damage your high-quality score if ads and marketing with Adwords.
Well I’m adding this RSS to my e-mail and could look out for a lot more of your respective exciting content.
Make sure you update this again very soon.|
Great blog here! Additionally your web site a
lot up fast! What host are you using? Can I get your associate link in your host?
I desire my website loaded up as quickly as yours lol
Hi it’s me, I am also visiting this web
site on a regular basis, this site is actually good and the users are in fact sharing good thoughts.
I’m more than happy to uncover this website. I need
to to thank you for your time due to this wonderful read!!
I definitely loved every part of it and i also
have you book-marked to see new things on your website.
Hi there, every time i used to check blog posts here early in
the morning, since i enjoy to learn more and more.
Having read this I thought it was very informative.
I appreciate you spending some time and effort to put this content together.
I once again find myself personally spending way too much time both reading and leaving comments.
But so what, it was still worth it!
Nice post. I was checking constantly this blog and I’m inspired!
Extremely useful information specially the remaining part
🙂 I handle such information a lot. I was seeking this certain info for
a long time. Thanks and good luck.
I гelish, result in I discovered just what I useԁ to be looking
for. Үou’ve enԁed my four day long hunt!
God Bless you man. Have a great day. Bye
Hi to every one, it’s genuinely a fastidious for me to pay a visit this web site, it contains valuable Information.
Good info. Lucky me I recently found your site by accident (stumbleupon).
I have saved it for later!
Thanks for sharing your thoughts. I truly appreciate your efforts and I will be waiting for your further post thank you once again.
Hi there I am so delighted I found your website, I really found you by mistake, while I was
looking on Yahoo for something else, Anyways I am here now and would just like to say thanks for a
incredible post and a all round enjoyable blog
(I also love the theme/design), I don’t have time to read through it all at the
minute but I have saved it and also added in your RSS
feeds, so when I have time I will be back to read more, Please do keep up
the excellent job.
Your style is so unique in comparison to other people I have read stuff from.
Thank you for posting when you’ve got the opportunity, Guess I’ll just book mark this site.
Heya great blog! Does running a blog such as this require a
great deal of work? I have virtually no expertise in computer
programming however I was hoping to start my own blog soon.
Anyhow, if you have any suggestions or tips for new blog owners please share.
I know this is off topic however I simply had to ask.
Cheers!|
I really like іt whenever people get together and share vіews.
Great website, continue the good work!
You could definitely see your skills within the article you write.
The world hopes for even more passionate writers like you who aren’t afraid
to mention how they believe. At all times follow your
heart.
Thank you a bunch ffor sharing this with all folks you actually realize what you’re speking about!
Bookmarked. Please alsao consult with my website =).
We can have a hyperlink trade agreement among us
I comment whenever I appreciate a article on a website
or I have something to valuable to contribute to
the discussion. Usually it is a result of the passion
communicated in the article I browsed. And on this post CoreData(マルチスレッドの処理 ) | Professional Programmer.
I was actually moved enough to drop a leave a responsea response 😉
I do have a few questions for you if it’s allright. Could it
be just me or does it look like like a few of the remarks appear like they are left by brain dead individuals?
😛 And, if you are writing at other places, I’d
like to keep up with you. Would you list every one of all your communal pages like your Facebook
page, twitter feed, or linkedin profile?
Your short post suggests that you’re rather literate and able to
write attention-catching blogposts. You write for other people?
I can also pay you for your work (on an hourly basis, per page, .etc).
Hey There. I found your blog using msn. This is a very
well written article. I will be sure to bookmark
it and return to read more of your useful information.
Thanks for the post. I will certainly return.
Thanks designed for sharing such a nice idea,
paragraph is pleasant, thats why i have read it fully
I am extremely impressed with your writing skills as well as with
the layout on your weblog. Is this a paid theme or did you customize it yourself?
Either way keep up the excellent quality writing, it is rare to see a
great blog like this one nowadays.
Valuable info. Fortunate me I found your website by chance, and I’m stunned why
this coincidence didn’t came about in advance! I bookmarked it.
Thank you, I’ve just been searching for info about this topic for a long time and yours
is the greatest I have discovered so far. But, what in regards
to the bottom line? Are you sure concerning the supply?
Keep on writing, great job!
This web site definitely has all the info I wanted about this subject and
didn’t know who to ask.
I’ll immediately grasp your rss as I can not find your e-mail subscription
hyperlink or newsletter service. Do you’ve any? Please let me
understand so that I could subscribe. Thanks.|
Thanks designed for sharing such a pleasant
opinion, article is good, thats why i have read it entirely|
It’s a shame you don’t have a donate button! I’d certainly donate to this outstanding blog!
I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
I look forward to brand new updates and will share this website with
my Facebook group. Chat soon!
If some one wants expert view on the topic of blogging and
site-building afterward i propose him/her to pay a visit
this webpage, Keep up the good job.
It’s difficult to find experienced people about this topic, but you sound like you know what you’re talking about!
Thanks
Its such as you read my mind! You seem to grasp
so much approximately this, like you wrote the e-book in
it or something. I think that you can do with a few percent to force the message
house a little bit, but other than that, this is
fantastic blog. An excellent read. I’ll certainly be back.