gpt4 book ai didi

iphone - performBlock 在哪个线程上运行?

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:26 25 4
gpt4 key购买 nike

确实是一个简单的问题,我的核心数据 UIManagedDocument 是在主线程上创建的,我想知道的是当我调用 performBlock 时是否在主线程(创建托管文档的同一线程)上阻塞队列/运行?

dispatch_queue_t backgroundQueue = dispatch_queue_create("DownloadQueue", NULL);
dispatch_async(backgroundQueue, ^{
// Get data from web
[document.managedObjectContext performBlock:^{
// Add data to Core Data
}];
});

最佳答案

您可以阅读苹果发行说明:CoreData Release Notes for iOS5

You can use contexts using the confinement pattern just as you have prior to OS X v10.7 and iOS 5. You send the contexts messages directly; it’s up to you to ensure that you send the messages from the right queue.

You use contexts using the queue-based concurrency types in conjunction with two new methods: performBlock: and performBlockAndWait:. You group “standard” messages to send to the context (including initialization such as setting the persistent store coordinator and so on) within a block to pass to one of these methods. The one exception is: if your code is executing on the main thread, you can invoke methods on the main queue style contexts directly instead of using the block based API.

performBlock: and performBlockAndWait: ensure the block operations are executed on the queue specified for the context. The performBlock: method returns immediately and the context executes the block methods on its own thread. With the performBlockAndWait: method, the context still executes the block methods on its own thread, but the method doesn’t return until the block is executed.

It’s important to appreciate that blocks are executed as a distinct body of work. As soon as your block ends, anyone else can enqueue another block, undo changes, reset the context, and so on. Thus blocks may be quite large, and typically end by invoking save:.

关于iphone - performBlock 在哪个线程上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12735213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com