有没有办法找出NSManagedObjectContext在哪个线程上? [英] Is there a way to figure out what thread an NSManagedObjectContext is on?

查看:85
本文介绍了有没有办法找出NSManagedObjectContext在哪个线程上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对与NSManagedObjectContext有关的线程的理解是,它只能在创建线程的线程上执行核心数据获取请求,删除等操作.有什么方法可以检查NSManagedObjectContext是在哪个线程上创建的,或者在特定的执行点上当前线程是否是特定的NSManagedObjectContext的线程?

My understanding of threads with respect to an NSManagedObjectContext is that it can only execute core data fetch requests, deletes, etc., on the thread from which it was created. Is there any way to check to see what thread an NSManagedObjectContext was created on, or if at a particular point of execution the current thread is that of a particular NSManagedObjectContext?

谢谢!

推荐答案

我对与NSManagedObjectContext相关的线程的理解是,它只能在创建线程的线程上执行核心数据获取请求,删除等操作.

My understanding of threads with respect to an NSManagedObjectContext is that it can only execute core data fetch requests, deletes, etc., on the thread from which it was created.

那不是很准确.最好说上下文不能由多个线程或队列同时使用.解决此问题的常用方法是为每个线程/队列创建不同的上下文.也可以使用performBlockperformBlockAndWait方法在多个线程上使用上下文,同时使上下文访问有效地保持为单线程.

That's not really accurate. It would be better to say that contexts cannot be used concurrently by more than one thread or queue. A common approach to dealing with this is to create different contexts for each thread/queue. It's also possible to use the performBlock and performBlockAndWait methods to use contexts on multiple threads while keeping context access effectively single-threaded.

结果是,上下文没有任何属于线程或队列的概念,线程也没有对在其上创建的上下文的任何引用.

As a result, contexts don't have any notion of belonging to a thread or queue, nor do threads have any reference to contexts that were created on them.

如果按照每个线程或队列使用一个上下文的方法,则需要跟踪代码将在何处运行并使用适当的上下文.例如,当使用GCD时,为特定的调度队列创建一个上下文,并且仅在使用dispatch_async之类的东西在该队列上运行时才使用它.

If you follow the approach of one context per thread or queue, you need to keep track of where code will run and use the appropriate context. For example when using GCD, create a context for a specific dispatch queue, and only use it when you've used something like dispatch_async to run on that queue.

如果您确实想将上下文与队列链接,则可以使用自己的数据结构通过当前的NSOperationQueue或调度队列或,或其他.这很少需要,但是如果您找不到更好的技术,就有可能.

If you really want to link a context with a queue, you could use your own data structure to look up contexts from whatever concurrency scheme you're using-- by the current NSOperationQueue, or dispatch queue, or NSThread, or whatever. This is rarely needed, but it's a possibility if you can't find better techniques.

这篇关于有没有办法找出NSManagedObjectContext在哪个线程上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆