开发者

CoreData Multithreading Proper Store Deletion

Ok, here's my situation:

I've got an app which requires a user-account. While you're doing stuff like writing new comments, creating new Posts or reading a thread, every call to the server runs on a separate thread. More specifically: A new thread is created, makes a call to the server, gets an answer from the server and saves the items from the answer to the Core Data Store.

In order to do this, every thread creates his own insertionContext, but they all share the same storeCoordinator.

But there's one Problem: When someone does a logout from his accoun开发者_JAVA百科t, I have to delete the store, so if he logs in with another account, the stuff from the other account isn't in the coreDataStorage anymore.

But in Order to delete the Store, I have to make sure that there aren't any background Threads running anymore, because once they try to to save their stuff, they are sure to crash the app, since the store isn't valid anymore. To clarify: these background threads are NSOperations which are put in an NSOperationQueue and executed from there.

Now CoreData gives the NSOperationQueue a function called "cancelAllOperations" but according to the Documentation, running Operations aren't killed, but only send a cancel message... How do I use this cancel Message o_O So far i'm checking at some points whether my thread is canceled and if it is, I don't execute other stuff, but if I do stuff like:

NSError *saveError = nil;
if(!self.isCanceled)
  [insertionContext save:&saveError];

There is still the possibility that the Thread is being canceled between the if-check and the save.

So my question: How do I handle this properly? Is it a question of properly canceling the thread?


I think you should not cancel any operations since it does not kill the thread immediately. Why don't you manage all operations that are currently being executed? This way you can postpone persistent store deletion until all tasks complete (or delete it immediately if there are no operations in progress).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜